O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Globals

We've seen one of the cases multiple times before global string slices:

const MEEP: &'static str = "meep"; static SECOND_MEEP: &'static str = "meep2"; 

The lifetime needs to be specified here since Rust's type inference is only local, so we need to spell out the types for all globals. The static lifetime means that these values start existing when the program starts and go away when the program does. All the literal strings in Rust programs are static, and since &'static str is not the same type as &str, we get a type error if we don't explicitly specify the lifetime here.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required