簡體   English   中英

靜態或const中`&str`和`&'static str`有什么區別?

[英]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_MEEPCONST_LIFETIME_MEEP什么CONST_LIFETIME_MEEP STATIC_MEEPSTATIC_LIFETIME_MEEP什么STATIC_LIFETIME_MEEP

沒什么,沒有區別。 RFC 1623開始staticconst項中的引用自動為'static 這在Rust 1.17中生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM