Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active August 22, 2016 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/f668806669e55fbc8d018a10da268496 to your computer and use it in GitHub Desktop.
Save KodrAus/f668806669e55fbc8d018a10da268496 to your computer and use it in GitHub Desktop.
Rust References
fn main() {
//String is a (possibly) mutable, growable slice of UTF-8 characters
let mut mutable_string: String = String::from("my owned string");
//We can borrow a String as an immutable &str, because of the Deref trait
let borrowed_str: &str = &mutable_string;
//Vec is a (possibly) mutable, growable contiguous array of things
let mut mutable_vec: Vec<i32> = vec![1, 2, 3];
//We can borrow a Vec<T> as an immutable &[T], because of the Deref trait
let borrowed_slice: &[i32] = &mutable_vec[..];
//Question: Can we mutate mutable_string and mutable_vec now?
//Most things in Rust don't have a different base type for owned vs borrowed references
//We can use the Cow<'a, T> (clone on write) to work with borrowed and owned types
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment