Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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?

like image 475
Rajkumar Natarajan Avatar asked Apr 06 '18 03:04

Rajkumar Natarajan


1 Answers

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.

like image 188
Shepmaster Avatar answered Oct 07 '22 09:10

Shepmaster