Skip to content

Instantly share code, notes, and snippets.

@DewofyourYouth
Created February 6, 2021 19:44
Show Gist options
  • Save DewofyourYouth/c894d9e6adecfe007d22ac653de82f68 to your computer and use it in GitHub Desktop.
Save DewofyourYouth/c894d9e6adecfe007d22ac653de82f68 to your computer and use it in GitHub Desktop.
The basics of ownership in Rust (from the rust book).
fn main() {
{
// simple things like integers are copied b/c they are fixed in size
let x = 5;
let y = x;
println!("{} + {} = {}", x, y, x + y); // this works b/c ints are simple values with a known fixed size
// this is not true for things like strings.
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
let m = s; // this is not a copy, it is a move
println!("{}", m);
// println!("{}", s); <- this will no longer work
let c = m.clone(); // this clones instead of moves.
println!("{}", c);
println!("{}", m); // this still works because it's a clone (a deep copy)
take_ownership(c); // `c` is now owned by the function, no longer in scope.
// println!("{}", c); <- this will not work b/c `c` has been owned by the function
let m = take_and_give_back(m); // here we return the string to `m` (this is shadowing)
println!("{}", m); // so we can still call it here.
let (m, len) = calculate_length_returning_tuple(m); // tuple assignment (we are again shadowing `m`)
println!("The length of '{}' is {}.", m, len); // notice you can still call m
let len = calculate_length_with_referencing(&m); // this uses a reference to m, still in the main fn scope.
println!("The length of '{}' is {}.", m, len); // notice you can still call m
make_a_copy(x);
let mut name = String::from("Jacob");
add_last_name(&mut name); // you can pass in a mutable reference if you want to change a variable and still make in available in scope.
println!("My name is {}!", name);
println!("x ( = {}) is still in scope!", x);
}
// this is out of scope and thus won't work
// println!("{}", s); // the memory s occupied was returned
}
fn make_a_copy(some_num: i32) {
println!("{} has been copied!", some_num);
}
fn take_ownership(some_string: String) {
println!("{} has been appropriated!", some_string)
}
// note that this function returns a string.
fn take_and_give_back(some_string: String) -> String {
println!("{} has been appropriated!", some_string);
some_string
}
// one way to return something and you want to return the variable to scope, you can return a tuple.
fn calculate_length_returning_tuple(s: String) -> (String, usize) {
let length = s.len();
(s, length)
}
// this just "borrows" the string - w/o taking ownership
fn calculate_length_with_referencing(s: &String) -> usize {
s.len() // if you tried to modify the string in this function, it wouldn't work b/c it's just borrowed.
}
// this also just "borrows" the string - w/o taking ownership
fn add_last_name(s: &mut String){
s.push_str(" Shore"); // here we mutate the string - but it works b/c we passed in a mutable reference.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment