简体   繁体   中英

What is the difference between `&str` and `&'static str` in a static or const?

I'm new to Rust programming and learning about lifetimes.

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);
}

The output:

CONST_MEEP is MEEP
CONST_LIFETIME_MEEP is MEEP
STATIC_MEEP is MEEP
STATIC_LIFETIME_MEEP is MEEP

What is the difference between CONST_MEEP and CONST_LIFETIME_MEEP ? What is the difference between STATIC_MEEP and STATIC_LIFETIME_MEEP ?

Nothing, there is no difference. As of RFC 1623 , references in static and const items are automatically 'static . This took effect in Rust 1.17 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM