【Rust】生存期-函数
环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime/fn.html
示例
main.rs
fn print_one<'a>(x: &'a i32) {
println!("`print_one`: x is {}", x);
}
// 可变的变量生存期标注
fn add_one<'a>(x: &'a mut i32) {
*x += 1;
}
// 有两个生存期标注
fn print_multi<'a, 'b>(x: &'a i32, y: &'b i32) {
println!("`print_multi`: x is {}, y is {}", x, y);
}
// 带有返回值的生存期参数
fn pass_x<'a, 'b>(x: &'a i32, _: &'b i32) -> &'a i32 {
x
}
fn main() {
let x = 7;
let y = 9;
print_one(&x);
print_multi(&x, &y);
let z = pass_x(&x, &y);
print_one(z);
let mut t = 3;
add_one(&mut t);
print_one(&t);
}
总结
了解了 Rust 中的函数的生存期参数的标注。