mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-04-22 21:48:04 +08:00
15 lines
374 B
Rust
15 lines
374 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 是 String 的引用
|
||
s.len()
|
||
} // 这里,s 离开了作用域。但因为它并不拥有引用值的所有权,
|
||
// 所以什么也不会发生
|
||
// ANCHOR_END: here
|