[英]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.