Skip to content

Instantly share code, notes, and snippets.

@KodrAus

KodrAus/playground.rs

Last active Aug 22, 2016
Embed
What would you like to do?
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
You can’t perform that action at this time.