2024-06-18 14:50:01 +08:00

24 lines
681 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// lifetimes1.rs
//
// Rust編譯器需要知道如何檢查提供的引用是否有效以便讓程式設計師知道引用是否有在使用前超出範圍的風險。記住引用是借用不擁有它們自己的值。如果它們的所有者超出範圍怎麼辦
//
// 執行 `rustlings hint lifetimes1` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is '{}'", result);
}