Michael, may I ask you a question regarding Exercise 1 References (see below).
Exercise 1 has two possible answers *x==5 and x==&5.I do understand the logic behind why these are the correct answer to the exercise, what I do not fully grasp is how all works in the case of x==&5.I will try to explain my reasoning:
- I believe that &5 is the memory address - for example 0x7fff9575c05f - of where a value 5 is stored
- when passing &5 to the function is_five, x is assigned the value of that memory address, so now we have x=0x7fff9575c05f.
- if x is &5, then if we substitute x for &5 in the expression x==&5, then we get &5==&5, which seem should give as result 'true'
- what I do not fully grasp is why the address of each &5 in the expression &5==&5 is the same, are not each 5 like two independent instances of a value 5 with their own unique address, so each address is different and therefore &5==&5 should be false.
What am I missing out or got mixed up ?P.D. I have tried to post my question on the platform and not found my way around on how to do it, it is not intuitive enough for me============================================================================================Exercise 1 #
The
is_five
function below has a bug. You can fix it by either adding in a borrow (&
) or deref (*
). Try fixing it both ways.fn is_five(x: &i32) -> bool { x == 5 } fn main() { assert!(is_five(&5)); assert!(!is_five(&6)); println!("Success!"); }El jue, 1 jul 2021 a las 15:29, Michael Snoyman (<mic...@snoyman.com>) escribió:My pleasure!On Thu, Jul 1, 2021, at 2:58 PM, Alejandro Buffery wrote:Thanks Michael for such a prompt and clear exemplification !