目录生命周期深入生命周期&'static和T:'static函数式编程:闭包、迭代器闭包Closure迭代器Iterator生命周期深入生命周期什么是生命周期?生命周期是Rust中用来保证引用有效性的工具。它确保了在任何时刻,所有引用都指向有效的内存。为
为什么需要生命周期?
声明与使用
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
'a是一个生命周期参数,它被绑定到函数的输入参数 x 和 y 的生命周期上。所有权和借用规则
fn example() {
let s = String::from("hello");
let r1 = &s; // r1 的生命周期被 s 的生命周期所限制
let r2 = &s; // r2 同样受限于 s 的生命周期
println!("r1: {}, r2: {}", r1, r2);
}
// 编译器会确保 r1 和 r2 在 s 有效期内有效
多个生命周期参数 当函数或结构体有多个引用时,需要明确指定不同的生命周期参数。
fn longest_with_another<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
// 使用示例
fn main() {
let string1 = String::from("long string is long");
let string2 = String::from("xyz");
let result = longest_with_another(&string1[..], &string2);
println!("The longest string is {}", result);
}
编译器自动推断
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() { x } else { y }
}
// 编译器可以自动推断生命周期
fn main() {
let string1 = String::from("long string is long");
let string2 = String::from("xyz");
let result = longest(&string1[..], &string2);
println!("The longest string is {}", result);
}
特殊之处
'static 是一个特殊的生命周期,表示“全局有效”。'static 生命周期。let s = "hello"; // 字面量字符串,生命周期为 'static
let static_str: &'static str = "hello";
解释
'static 生命周期时,意味着 T 必须能够存在于整个程序运行期间。实际应用
// 定义一个函数,接受一个实现了 'static 生命周期的类型 T 的引用
fn print_lifetime<T: 'static>(t: &T) {
println!("{:?}", t);
}
// 使用示例
print_lifetime(&"hello"); // OK
print_lifetime(&5); // OK
print_lifetime(&vec![1, 2, 3]); // 编译错误,因为 Vec 不实现 'static
全局变量
'static 生命周期。'static 生命周期。static HELLO: &str = "hello";
fn main() {
println!("{}", HELLO);
}
动态分配内存
'static 生命周期。'static 生命周期。fn create_box() -> Box<dyn Any + 'static> {
Box::new(42)
}
fn main() {
let box_value = create_box();
println!("{:?}", box_value);
}
字符串字面量
'static 生命周期。'static 生命周期的函数。fn print_static_str(s: &'static str) {
println!("{}", s);
}
fn main() {
let s = "hello";
print_static_str(s);
}
类型约束 T: 'static
trait MyTrait {}
impl MyTrait for i32 {}
impl MyTrait for String {}
fn process_data<T: MyTrait + 'static>(data: T) {
println!("{:?}", data);
}
fn main() {
process_data(42); // OK
process_data(String::from("hello")); // 编译错误,String 不实现 'static
}
动态调度
Box<dyn Trait> 时,需要确保类型具有 'static 生命周期。
trait MyTrait {
fn say_hello(&self);
}
impl MyTrait for i32 {
fn say_hello(&self) {
println!("Hello from an integer!");
}
}
impl MyTrait for String {
fn say... 如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!