Last active
August 22, 2016 10:28
-
-
Save KodrAus/f668806669e55fbc8d018a10da268496 to your computer and use it in GitHub Desktop.
Rust References
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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