Box Smart Pointer

Exercises

Creation

// Initialize heap_var to store value 4 on the heap & make the code execute successfully.

fn main() {
    let stack_var = 5;
    // TODO: initialize this variable
    let heap_var
    let res = stack_var + *heap_var;
    assert_eq!(res, 9);
}
Solution
fn main() {
  let stack_var = 5;
  let heap_var = Box::new(4);
  let res = stack_var + *heap_var;
  assert_eq!(res, 9);
}

Recursive types

Description

This is a helpful description. Read me to understand what to do!

// The recursive type we're implementing in this exercise is the `cons list` - a data structure
// frequently found in functional programming languages. Each item in a cons list contains two
// elements: the value of the current item and the next item. The last item is a value called `Nil`.
//
// Step 1: use a `Box` in the enum definition to make the code compile
// Step 2: create both empty and non-empty cons lists by replacing `todo!()`


#[derive(PartialEq, Debug)]
pub enum List {
    Cons(i32, List),
    Nil,
}

fn main() {
    println!("This is an empty cons list: {:?}", create_empty_list());
    println!(
        "This is a non-empty cons list: {:?}",
        create_non_empty_list()
    );
}

pub fn create_empty_list() -> List {
    todo!()
}

pub fn create_non_empty_list() -> List {
    todo!()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_empty_list() {
        assert_eq!(List::Nil, create_empty_list())
    }

    #[test]
    fn test_create_non_empty_list() {
        assert_ne!(create_empty_list(), create_non_empty_list())
    }
}
Solution
#[derive(PartialEq, Debug)]
pub enum List {
  Cons(i32, Box<List>),
  Nil,
}

fn main() {
  println!("This is an empty cons list: {:?}", create_empty_list());
  println!(
      "This is a non-empty cons list: {:?}",
      create_non_empty_list()
  );
}

pub fn create_empty_list() -> List {
  List::Nil
}

pub fn create_non_empty_list() -> List {
  List::Cons(1, Box::new(List::Cons(1, Box::new(List::Nil))))
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_create_empty_list() {
      assert_eq!(List::Nil, create_empty_list())
  }

  #[test]
  fn test_create_non_empty_list() {
      assert_ne!(create_empty_list(), create_non_empty_list())
  }
}