Constants & Statics

Additional Resources

Exercises

Constants

// Fix the code so it compiles!

const NUMBER = 3;

fn main() {
    println!("Number {}", NUMBER);
}
Solution
const NUMBER: i32 = 3;

fn main() {
  println!("Number {}", NUMBER);
}

Statics

// Fix the code so it compiles!

static LANGUAGE = "Go";

fn main() {
    // These two initializations perform a string copy from same memory location
    let lang1 = LANGUAGE;
    let mut lang2 = LANGUAGE;
    
    lang2 = "Rust";
    println!("I like {} more than {}!", lang2, lang1);
}
Solution
static LANGUAGE: &str = "Go";

fn main() {
  // These two initializations perform a string copy from same memory location
  let lang1 = LANGUAGE;
  let mut lang2 = LANGUAGE;
  
  lang2 = "Rust";
  println!("I like {} more than {}!", lang2, lang1);
}