Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upborrow/ref: Not clear what's the difference between ref and & #390
Comments
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 11, 2015
|
Why can't you use match a {
Some(&s) => {}
}? e.g. in methods I can use |
This comment has been minimized.
This comment has been minimized.
|
That, I don't know. It's not really a decision I agree with. I like that On Sun, Jan 11, 2015 at 11:27 AM, Kornel notifications@github.com wrote:
"Calling all. This is our last cry before our eternal silence." |
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 11, 2015
|
I'm not trying to question Rust design decision here, just understand difference between Whether that's just arbitrary, or quirky syntax to avoid ambiguity, or it has slightly different meaning, I'd like to know that, because I'm not sure when to use which and why rustc complains about |
This comment has been minimized.
This comment has been minimized.
|
I think the idea is that you destruct as you construct, like in a let op = Some(a);
So if you type
On Jan 11, 2015 11:44 AM, "Kornel" notifications@github.com wrote:
|
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 12, 2015
|
Sorry, I don't understand your last answer. Perhaps I'm asking stupid questions, but the mental model I have is from C:
and there's no room in it for
then in my mind it's identical with When I use
I'm not sure what |
This comment has been minimized.
This comment has been minimized.
|
So where are you seeing this error? In Rust, you basically have this:
And in pattern matching, you do this:
What about when we have to get a reference in a match statement where there was
However, what if you have, say, a match statement?
(Note: this is taken directly from the rust docs book) Does that make sense? |
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 12, 2015
|
The error I was getting was from match a {
Some(&x) =>
}So I think I'm starting to get why it's illegal syntax: fn main() {
let x = 123;
let &x_ref_1 = &x;
let ref x_ref_2 = &x;
//let & x_ref_3 = x; // same error as when matching &x
let ref x_ref_4 = x;
}It would be great if the tutorial explained exactly what happens in the cases above (they all print the same value, but I suppose Rust's automagic dereferencing hides the differences). but "matching uses same syntax as constructing" doesn't seem right to me: let s1 = Some(&x); // legal
let s2 = Some(ref x); // illegal |
This comment has been minimized.
This comment has been minimized.
|
There are two things going on here:
|
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 12, 2015
|
Oh, that's interesting! Are these two statements equivalent? let x = &y;
let ref x = y; |
This comment has been minimized.
This comment has been minimized.
|
Yes, exactly! On Mon, Jan 12, 2015 at 2:23 PM, Kornel notifications@github.com wrote:
"Calling all. This is our last cry before our eternal silence." |
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 12, 2015
|
Ok, so now it makes sense to me! Thanks! I suggest adding such simple example to the site: let y = 'y';
// `ref` on the left side of an assignment is like adding `&` on the right side
let ref x1 = y;
let x2 = &y;
println!("{}", x1 == x2);The full example is useful as well, but with destructuring at the same time there's much more going on. |
This comment has been minimized.
This comment has been minimized.
|
Thank you very much for bringing this incredibly confusing thing to our
|
This comment has been minimized.
This comment has been minimized.
|
@pornel I just saw this thread and modified PR #421 to try to elaborate on this. The specific file was pointers.rs By the way, your critiques are very helpful. It's really hard to anticipate learning questions. Generally I focus on trying to fix things I find confusing but having others do the same is really useful. |
mdinger
referenced this issue
Jan 20, 2015
Merged
Move all flow control items into `flow control` group. Add `if let` explanations. #421
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Jan 20, 2015
|
@mdinger thanks. It's much clearer. And I didn't know if let existed :) |
This comment has been minimized.
This comment has been minimized.
|
These explanations are missing in general and really need to be added. |
This comment has been minimized.
This comment has been minimized.
|
Destructuring and pattern matching confusion discussed here. I have a feeling pattern matching may need to be discussed as a separate topic from |
This comment has been minimized.
This comment has been minimized.
|
Is this still a problem? Is there an actionable response that can be taken? |
This comment has been minimized.
This comment has been minimized.
kornelski
commented
Aug 9, 2015
|
Yes, I think it still can be improved. The big example on that page focuses on a case where For me it "clicked" when I saw the simplest case: // `ref` on the left side of an assignment is like adding `&` on the right side
let ref x1 = y;
let x2 = &y;So I suggest adding exactly that to the page, before showing a complex case. |
steveklabnik
closed this
in
#629
Aug 11, 2015
This comment has been minimized.
This comment has been minimized.
LukeSkyw
commented
Feb 25, 2017
|
Just spent two hours trying to make sense of this:
So, as explained above, it should have been:
|
This comment has been minimized.
This comment has been minimized.
AlecZadikian9001
commented
May 6, 2017
•
|
I find it strange that |
This comment has been minimized.
This comment has been minimized.
abbshr
commented
Aug 30, 2017
•
fn main() {
if let Some(&pat) = Some(&Box::new(1)) {
println!("{:?}", pat);
}
}The rust compiler explained as follow:
It says "cannot move out of borrowed content, to prevent move, use Besides, I think |
kornelski commentedJan 11, 2015
In other languages
&is called a reference and borrowing looks very much like references, so to me&andrefseem like the same thing.I'd appreciate an example that contrasts the two — why
let Point { x: ref ref_to_x, y: _ }is OK, butlet Point { x: & ref_to_x, y: _ }isn't?