Created
October 27, 2021 08:07
-
-
Save ChristianChiarulli/a7cbbad2d8fe933de6155f17d0e74e43 to your computer and use it in GitHub Desktop.
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() { | |
| 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