-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifetimes.rs
More file actions
31 lines (26 loc) · 1.04 KB
/
lifetimes.rs
File metadata and controls
31 lines (26 loc) · 1.04 KB
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
//! lifetimes.rs
//!
//! Introduction to Rust lifetimes — one of the most important concepts
//! for understanding references and borrowing. Shows why lifetimes exist
//! and how the compiler uses them.
fn main() {
// Example 1: A simple function that takes a reference
let text: &str = "Hello, Rust lifetimes!";
let result: &str = longest_word(text, "short");
println!("The longest word is: {}", result);
// Example 2: Multiple references with explicit lifetime annotation
let string1: String = String::from("long string here");
let string2: String = String::from("short");
let longest: &str = longest_word(&string1, &string2);
println!("Longest between two strings: {}", longest);
}
// This function takes two string slices and returns a reference to the longest one.
// The lifetime 'a tells the compiler that the returned reference will live
// at least as long as the shortest input reference.
fn longest_word<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}