Skip to content

Instantly share code, notes, and snippets.

@ChristianChiarulli
Created October 27, 2021 08:07
Show Gist options
  • Select an option

  • Save ChristianChiarulli/a7cbbad2d8fe933de6155f17d0e74e43 to your computer and use it in GitHub Desktop.

Select an option

Save ChristianChiarulli/a7cbbad2d8fe933de6155f17d0e74e43 to your computer and use it in GitHub Desktop.
fn main() {
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
vec_loop(v);
}
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
print_type_of(&i);
print_type_of(&*i);
// *i = *i * 2;
// let's clear up some confusion
// printing just i and *i with {} will both give the value
// why doesn't i give the address?
// it just doesn't, rust thinks giving you the value is more important I guess
// but you can get the address with {:p}
// consequently *i will throw and error if you try to print with {:p} since it is just the
// value and doesn't also contain the address data i contains both
// basically i (&mut i32) has the Pointer trait *i (i32) does not
println!("{:p}", i);
// println!("{:p}", *i); // this will give a compiler error
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment