CMSC330

Rust

Rust

Slices
Structs
Traits
Boxes and RC

Slices

Slices

Recall the following memory issue


char* str = "hello world";
char* sub = *str[5]
free(str);
printf("%s",sub);
          

Similar in Rust


let x = String::from ("Hello World");
let y = &x[6..10];
drop(x);
println!("P{",y);
          

Rust will not compile this

Slices reference a part of a continuous piece of memeory


let x = String::from ("Hello World");
let hello = &s[0..5];
let world = &s[6..11];

x: String
  ptr -> Hello World
  len: 11
  capacity: 11

hello: &str
  ptr -> x.ptr
  len: 5

hello: &str
  ptr -> x.ptr[6]
  len: 5
          

If x is dropped, so is everything else

x cannot be modifed while slices exist

Slices reference a part of a continuous piece of memeory


          let a = [1,2,3,4];
          let v = Vec::new();;
          v.push(1);
          let b = &a[1..3]
          let w = &v[..2]
          

Arrays and vectors also have slices

Structs


struct User {
    username: String,
    active: bool,
}
let user1 = User {
  username: String::from("clyff"),
  active: true,
}
println!("{}",user1.username);
          

Mutability describes a variable, not the data


let mut x = User { ... };
x.active = false;
          

struct User {
    username: String,
    active: bool,
}
let user1 = User {
  username: String::from("clyff"),
  active: true,
}
println!("{}",user1.username);
          

Structs have associated methods


struct User {
    username: String,
    active: bool,
}

impl{
  fn is_online(&self){
    self.active
  }
}
          

Enums also are helpful for custom data types


enum Color{
  Green,
  Red,
  Blue,
  Other(String)
}

let r = Color::Red;
let o = Color::Other(String::from("Grey"));
          

Enums also have associated methods


impl Color{
  fn is_green(&self)->boool{
    match self{
      Color::green => true,
      _ => false,
    }
  }
}
          

Both Enums and Structs allow for Generics


struct Point<T>{
  x:T,
  y:T,
}

enum option<T>{
  None,
  Some(T),
}
          

Same for associated Functions


impl<T > Point<T>{
  fn x(&self) -> &T{
    &self.x
  }
}
          

Hashmaps are built into Rust


let x = HashMap<i32,String>::new();
          

Traits

Like an Java intrerface


pub trait Printable{
  fn print(&self) -> String;
}

impl Printable for User{
  fn print(&self){
    format!("{}:{}",self.username,self.active)
  }
}

impl Printable for Point{
  fn print(&self){
    format!("({},{})",self.x,self.y)
  }
}
          

default implemention: so also like abstract class


pub trait Printable{
  fn print(&self) -> String{
    "I was supposed to have this overridden"
  }
}
          

Can have multiple and on a generic


struct Point<T:Clone + PartialOrd>{
  x:T,
  y:T,
}
          

Common Traits

  • Copy
  • Clone
  • Move
  • Deref
  • Display

Lifetimes are an implicit generic

Lifetime: scope for which a reference is valid


        let r;
        {
            let x = 5;
            r = &x;
        }
        println!("r: {}", r);
          

r and x have different lifetimes

Lifetimes are an implicit generic


fn longest(x: &str, y: &str) -> &str {
  if x.len() > y.len() {
    x
  } else {
    y
  }
}

Return type has no gaurantee it's a valid reference


fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
  if x.len() > y.len() {
    x
  } else {
    y
  }
}

struct ImportantString<'a>{
  istring: &'a str,
}
          

The instance of ImportantString cannot outlive the reference it holds


let x = String::from("Hello world");
{
  let y = ImportantString {istring:&x };
}
          

Boxes and RC


enum Tree{
  Leaf,
  Node(i32,Tree,Tree),
}
          

Issue: compiler does not know the size of Tree


enum Tree{
  Leaf,
  Node(i32,Box<Tree>,Box<Tree>),
}
          

Box has Deref and Drop traits

Box has Deref and Drop traits


let x = 5;
let y = Box::new(x);

assert_eq!(5, x);
assert_eq!(5, *y);