RUST 静态生命周期和动态生命周期

📅 2026/7/27 19:15:07 👁️ 阅读次数 📝 编程学习
RUST 静态生命周期和动态生命周期

图例

分配在堆和栈上的内存有其各自的作用域,它们的生命周期是动态的

全局变量、静态变量、字符串字母量、代码等内容,在编译时,会被编译到可执行文件中的 BSS/Data/RoData/Text 段,然后在加载时,装入内存

因而,它们的生命周期和进程的生命周期一致,所以静态的

所以,函数指针的生命周期也是静态,因为函数在Text 段中,只要进程活着,其内存一直存在

静态生命周期

概述

静态生命周期 Rust 中最长的生命周期,表示引用在整个程序运行期间都有效。这些数据通常存储在程序的只读内存区域,在程序启动时创建,在程序结束时销毁

特点

  1. 数据存在于整个程序运行期间
  2. 通常存储在静态内存区域
  3. 不需要运行时生命周期检查
  4. 最安全的引用类型

示例

// 字符串字面量具有 'static 生命周期letstatic_str:&'staticstr="Hello, World!";// 静态变量也具有 'static 生命周期staticSTATIC_INT:i32=42;letstatic_ref:&'statici32=&STATIC_INT;// 函数返回 'static 生命周期fnget_static_str()->&'staticstr{"This string lives for the entire program"}// 在结构体中使用 'static 生命周期structStaticHolder{data:&'staticstr,}implStaticHolder{fnnew()->Self{StaticHolder{data:"This data lives forever",}}}fnmain(){letholder=StaticHolder::new();println!("{}",holder.data);lets=get_static_str();println!("{}",s);// 这些引用可以在任何地方使用,不会出现悬垂引用letlong_lived_ref=static_str;println!("{}",long_lived_ref);}

动态生命周期

概述

动态生命周期是指哪些在编译时无法确定具体持续时间,需要在运行时通过检查器验证的生命周期

这些生命周期通常与作用域相关,由编译器推断或通过生命周期参数明确指定

特点

  1. 生命周期在编译不确定,需要运行时检查
  2. 与特定作用域绑定
  3. 需要生命周期注解来帮助编译器验证安全性
  4. 更灵活,但需要更多编译器支持

例子

// 函数接受两个引用并返回一个引用,需要明确生命周期参数fnlongest<'a>(x:&'astr,y:&'astr)->&'astr{ifx.len()>y.len(){x}else{y}}// 结构体包含引用,需要生命周期参数structTextEditor<'a>{content:&'astr,selection:&'astr,}impl<'a>TextEditor<'a>{fnnew(content:&'astr)->Self{TextEditor{content,selection:"",}}fnselect(&mutself,start:usize,end:usize){self.selection=&self.content[start..end];}}fnmain(){// 动态生命周期的示例letstring1=String::from("long string is long");letstring2=String::from("short");letresult;{// 这两个引用有不同的生命周期letpart1=&string1;letpart2=&string2;// longest 函数要求两个参数有相同的生命周期 'aresult=longest(part1,part2);println!("The longest string is: {}",result);}// part1 和 part2 在这里离开作用域// result 仍然有效,因为它引用的是 string1 或 string2 的一部分// 而 string1 和 string2 仍然在作用域内println!("Still can use: {}",result);// 文本编辑器示例letcontent=String::from("This is a long text content for the editor");letmuteditor=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)动态生命周期
持续时间整个程序运行期间由作用域决定
内存位置静态内存区域栈或堆
安全性绝对安全需要编译器验证
灵活性
使用场景字符串字面量、静态变量函数参数、结构体字段