RUST 静态生命周期和动态生命周期
图例
分配在堆和栈上的内存有其各自的作用域,它们的生命周期是动态的
全局变量、静态变量、字符串字母量、代码等内容,在编译时,会被编译到可执行文件中的 BSS/Data/RoData/Text 段,然后在加载时,装入内存
因而,它们的生命周期和进程的生命周期一致,所以静态的
所以,函数指针的生命周期也是静态,因为函数在Text 段中,只要进程活着,其内存一直存在
静态生命周期
概述
静态生命周期 Rust 中最长的生命周期,表示引用在整个程序运行期间都有效。这些数据通常存储在程序的只读内存区域,在程序启动时创建,在程序结束时销毁
特点
- 数据存在于整个程序运行期间
- 通常存储在静态内存区域
- 不需要运行时生命周期检查
- 最安全的引用类型
示例
// 字符串字面量具有 'static 生命周期
let static_str: &'static str = "Hello, World!";// 静态变量也具有 'static 生命周期
static STATIC_INT: i32 = 42;
let static_ref: &'static i32 = &STATIC_INT;// 函数返回 'static 生命周期
fn get_static_str() -> &'static str {"This string lives for the entire program"
}// 在结构体中使用 'static 生命周期
struct StaticHolder {data: &'static str,
}impl StaticHolder {fn new() -> Self {StaticHolder {data: "This data lives forever",}}
}fn main() {let holder = StaticHolder::new();println!("{}", holder.data);let s = get_static_str();println!("{}", s);// 这些引用可以在任何地方使用,不会出现悬垂引用let long_lived_ref = static_str;println!("{}", long_lived_ref);
}
动态生命周期
概述
动态生命周期是指哪些在编译时无法确定具体持续时间,需要在运行时通过检查器验证的生命周期
这些生命周期通常与作用域相关,由编译器推断或通过生命周期参数明确指定
特点
- 生命周期在编译不确定,需要运行时检查
- 与特定作用域绑定
- 需要生命周期注解来帮助编译器验证安全性
- 更灵活,但需要更多编译器支持
例子
// 函数接受两个引用并返回一个引用,需要明确生命周期参数
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {if x.len() > y.len() {x} else {y}
}// 结构体包含引用,需要生命周期参数
struct TextEditor<'a> {content: &'a str,selection: &'a str,
}impl<'a> TextEditor<'a> {fn new(content: &'a str) -> Self {TextEditor {content,selection: "",}}fn select(&mut self, start: usize, end: usize) {self.selection = &self.content[start..end];}
}fn main() {// 动态生命周期的示例let string1 = String::from("long string is long");let string2 = String::from("short");let result;{// 这两个引用有不同的生命周期let part1 = &string1;let part2 = &string2;// longest 函数要求两个参数有相同的生命周期 'aresult = longest(part1, part2);println!("The longest string is: {}", result);} // part1 和 part2 在这里离开作用域// result 仍然有效,因为它引用的是 string1 或 string2 的一部分// 而 string1 和 string2 仍然在作用域内println!("Still can use: {}", result);// 文本编辑器示例let content = String::from("This is a long text content for the editor");let mut editor = TextEditor::new(&content);editor.select(5, 15);println!("Selection: {}", editor.selection);// 如果尝试在 content 离开作用域后使用 editor,会导致编译错误// {// let local_content = String::from("local");// let local_editor = TextEditor::new(&local_content);// // local_editor 不能离开这个作用域,因为它的引用依赖于 local_content// }// println!("{:?}", local_editor); // 这会编译错误
}
总结
特性 | 静态生命周期 ('static) | 动态生命周期 |
---|---|---|
持续时间 | 整个程序运行期间 | 由作用域决定 |
内存位置 | 静态内存区域 | 栈或堆 |
安全性 | 绝对安全 | 需要编译器验证 |
灵活性 | 低 | 高 |
使用场景 | 字符串字面量、静态变量 | 函数参数、结构体字段 |