book/listings/ch04-understanding-ownership/no-listing-08-reference-wit.../src/main.rs

15 lines
378 B
Rust

fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{s1}' is {len}.");
}
// ANCHOR: here
fn calculate_length(s: &String) -> usize { // s is a reference to a String
s.len()
} // Here, s goes out of scope. But because it does not have ownership of what
// it refers to, it is not dropped.
// ANCHOR_END: here