fnmain() { 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 }; println!("{}",i.part); }
结合泛型、trait、生命周期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use std::fmt::Display;
fnlongest_with_an_announcement<'a, T>(x: &'astr, y: &'astr, ann: T) -> &'astr where T: Display { println!("Announcement! {}", ann); if x.len() > y.len() { x } else { y } }