mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-04-23 05:58:02 +08:00
15 lines
380 B
Rust
15 lines
380 B
Rust
|
fn main() {
|
||
|
let s1 = String::from("hello");
|
||
|
|
||
|
let len = calculate_length(&s1);
|
||
|
|
||
|
println!("The length of '{}' is {}.", s1, 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, nothing happens.
|
||
|
// ANCHOR_END: here
|