user51
user51

Reputation: 10163

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?

Upvotes: 16

Views: 4221

Answers (1)

Shepmaster
Shepmaster

Reputation: 430851

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.

Upvotes: 20

Related Questions