静态或const中`& str`和`&' static str`有什么区别?

时间:2018-04-06 03:05:29

标签: rust

我是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之间有什么区别? STATIC_MEEPSTATIC_LIFETIME_MEEP之间有什么区别?

1 个答案:

答案 0 :(得分:7)

没什么,没有区别。自RFC 1623起,staticconst项中的引用自动为'static。这在Rust 1.17生效。