Skip to content

Instantly share code, notes, and snippets.

@pnkfelix
Last active August 29, 2015 14:23
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 pnkfelix/b30f72fe3eb559c83994 to your computer and use it in GitHub Desktop.
Save pnkfelix/b30f72fe3eb559c83994 to your computer and use it in GitHub Desktop.
Exercises Part 1, Section 1
// The `main` function is traditional program entry point.
pub fn main() {
println!("Running exercises for section 1.1");
let vec0 = Vec::new();
// (*) (see exercises)
let mut vec1 = fill_vec(vec0);
// (**) (see exercises)
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
vec1.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
} // <-- Here, we hit the end of the scope for both `vec0` and `vec1`.
// The destructor for `Vec` will free any storage allocated to them.
// The `fill_vec` subroutine. Puts [22, 44, 66] into input vector,
// then returns it.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
// Move `vec` into new binding to make it mutable
// (***) (see exercises)
let mut vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
// transfer ownership of mutated `vec` back to caller
return vec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment