1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| use generic_types_traits_and_lifetimes::Summary; use generic_types_traits_and_lifetimes::Tweet; use std::fmt::Display;
fn largest_generic<T:std::cmp::PartialOrd + Clone>(list: &[T]) -> &T { let mut largest = &list[0];
for item in list { if item > largest { largest = item; } }
largest }
struct Point<T> { x: T, y: T, }
impl Point<i32> { fn selfx(&self) -> &i32 { &self.x } }
impl Point<f32> { fn distance_from_origin(&self) -> f32 { (self.x.powi(2) + self.y.powi(2)).sqrt() } }
impl Point<&str>{ fn concatenate(&self) -> String { format!("{}{}", self.x, self.y) } }
#[derive(Debug)] struct Point2<T, U> { x: T, y: U, }
impl<T, U> Point2<T, U> { fn mixup<V, W>(self, other: Point2<V, W>) -> Point2<T, W> { Point2 { x: self.x, y: other.y, } } }
struct _ImportantExcerpt<'a> { _part: &'a str, }
fn main() { let number_list = vec![34, 50, 25, 100, 65];
let result1 = largest(&number_list);
println!("The largest number is {}", result1);
let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];
let result2 = largest(&number_list);
println!("The largest number is {}", result2);
let str_list = vec!["Hello", "Rust", "World"]; let result3 = largest_generic(&str_list); println!("The largest string is {}", result3);
let integer = Point { x: 5, y: 10 }; println!("x,y = {},{}", integer.x, integer.y); let float = Point { x: 1.0, y: 4.0 }; println!("x,y = {},{}", float.x, float.y); let integer = Option::Some(5); let float = Option::Some(5.0); let none: Option<i32> = None; println!("integer = {:?}, float = {:?}, none = {:?}", integer, float, none); println!("integer = {:?}, float = {:?}, none = {:?}", integer, float, none);
let p1 = Point { x: 5, y: 10 }; let p2 = Point { x: "Hello", y: " Rust" }; let p3 = Point { x: 5.0, y: 10.0 }; println!("p1:{}",p1.selfx()); println!("p2:{}",p2.concatenate()); println!("p3:{}",p3.distance_from_origin());
let p4 = Point2 { x: 5, y: 10.4 }; let p5: Point2<&str, i32> = Point2 {x:"Hello", y:2}; println!("p4:{:?}",p4.mixup(p5));
let tweet = Tweet { username: String::from("horse_ebooks"), content: String::from("of course, as you probably already know, people"), reply: false, retweet: false, };
println!("1 new tweet: {}", tweet.summarize());
let x = 5; let r = &x; println!("r:{}",r);
let string1 = String::from("abcd"); let string2 = "xyz"; let result = longest(string1.as_str(), string2); println!("The longest string is {}", result);
let novel = String::from("Call me Ishmael. Some years ago..."); let first_sentence = novel.split('.').next().expect("Could not find a '.'"); let _i = _ImportantExcerpt { _part: first_sentence };
}
fn largest(list: &[i32]) -> &i32 { let mut largest = &list[0];
for number in list { if number > largest { largest = number; } }
largest }
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
fn _longest<'a>(x: &'a str, _y: &str) -> &'a str { x }
fn _corroct_longest<'a>(_x: &'a str, _y: &str) -> String { let result = String::from("really long string"); result }
fn _first_word(s: &str) -> &str { let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[0..i]; } }
&s[..] }
fn _longest_with_an_announcement<'a, T>( x: &'a str, y: &'a str, ann: T, ) -> &'a str where T: Display { println!("Announcement! {}", ann); if x.len() > y.len() { x } else { y } }
|