Concret Lifetimes

Exercises

Lifetimes of owned values

// Fix the code by shifting only one statement.

fn main() {
    let str1 = "🦀".to_string();
    let bytes = str1.into_bytes();
    println!("A crab: {str1}");
    println!("A crab represented in unicode: {bytes:?}");
}
Solution
fn main() {
  let str1 = "🦀".to_string();
  println!("A crab: {str1}");
  let bytes = str1.into_bytes();
  println!("A crab represented in unicode: {bytes:?}");
}

Dangling references

// Fix the code by addressing the TODO.

fn main() {
    let num_ref;
    {
        // TODO: shift below statement to appropriate location
        let num = 23;
        num_ref = #
    }
    println!("Reference points to {}", num_ref);
}
Solution
fn main() {
  let num_ref;
  let num = 23;
  {
      num_ref = #
  }
  println!("Reference points to {}", num_ref);
}

Non-lexical lifetimes

// Fix the code by shifting only one statement.

fn main() {
    let mut my_str = "Old String".to_owned();
    let ref1 = &my_str;
    let ref2 = &mut my_str;
    ref2.replace_range(0..3, "New");
    println!("{ref1}");
    println!("{ref2}");
}
Solution
fn main() {
  let mut my_str = "Old String".to_owned();
  let ref1 = &my_str;
  println!("{ref1}");
  let ref2 = &mut my_str;
  ref2.replace_range(0..3, "New");
  println!("{ref2}");
}