58 lines
1.2 KiB
Rust
58 lines
1.2 KiB
Rust
|
use std::fmt::Display;
|
||
|
|
||
|
struct ImportantExcerpt<'a> {
|
||
|
part: &'a str,
|
||
|
}
|
||
|
|
||
|
impl<'a> ImportantExcerpt<'a> {
|
||
|
fn level(&self) -> i32 {
|
||
|
3
|
||
|
}
|
||
|
|
||
|
fn announce_and_return_part(&self, announcement: &str) -> &str {
|
||
|
println!("Attention please: {}", announcement);
|
||
|
self.part
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let string1 = String::from("abcd");
|
||
|
let string2 = "xyz";
|
||
|
|
||
|
let result = longest(string1.as_str(), string2);
|
||
|
println!("The longest string is {}", result);
|
||
|
|
||
|
let string1 = String::from("long string is long");
|
||
|
|
||
|
{
|
||
|
let string2 = String::from("xyz");
|
||
|
let result = longest(string1.as_str(), string2.as_str());
|
||
|
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 longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||
|
if x.len() > y.len() {
|
||
|
x
|
||
|
} else {
|
||
|
y
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|