[英]What is the difference between `&str` and `&'static str` in a static or const?
我是Rust编程的新手,也是关于生命周期的学习。
const CONST_MEEP: &str = "MEEP";
const CONST_LIFETIME_MEEP: &'static str = "MEEP";
static STATIC_MEEP: &'static str = "MEEP";
static STATIC_LIFETIME_MEEP: &str = "MEEP";
fn main() {
println!("CONST_MEEP is {}", CONST_MEEP);
println!("CONST_LIFETIME_MEEP is {}", CONST_LIFETIME_MEEP);
println!("STATIC_MEEP is {}", STATIC_MEEP);
println!("STATIC_LIFETIME_MEEP is {}", STATIC_LIFETIME_MEEP);
}
输出:
CONST_MEEP is MEEP
CONST_LIFETIME_MEEP is MEEP
STATIC_MEEP is MEEP
STATIC_LIFETIME_MEEP is MEEP
CONST_MEEP
和CONST_LIFETIME_MEEP
什么CONST_LIFETIME_MEEP
? STATIC_MEEP
和STATIC_LIFETIME_MEEP
什么STATIC_LIFETIME_MEEP
?
没什么,没有区别。 从RFC 1623开始 , static
和const
项中的引用自动为'static
。 这在Rust 1.17中生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.