繁体   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