book/listings/ch10-generic-types-traits-a.../listing-10-20/src/main.rs

18 lines
325 B
Rust

fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is {result}");
}
// ANCHOR: here
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}
// ANCHOR_END: here