-
Notifications
You must be signed in to change notification settings - Fork 1.4k
borrow/ref: Not clear what's the difference between ref and & #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
Why can't you use match a {
Some(&s) => {}
} ? e.g. in methods I can use |
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." |
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 |
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:
|
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 |
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? |
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 |
There are two things going on here:
|
Oh, that's interesting! Are these two statements equivalent? let x = &y;
let ref x = y; |
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." |
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. |
Thank you very much for bringing this incredibly confusing thing to our
|
@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 thanks. It's much clearer. And I didn't know if let existed :) |
These explanations are missing in general and really need to be added. |
Destructuring and pattern matching confusion discussed here. I have a feeling pattern matching may need to be discussed as a separate topic from |
Is this still a problem? Is there an actionable response that can be taken? |
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. |
Just spent two hours trying to make sense of this:
So, as explained above, it should have been:
|
I find it strange that |
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 |
Hello, sorry for comment on old issue, but I found it's an interesting topic. I know that pattern match is a way to match programming language syntax itself, but I still feel confused about keyword For example: if let Some(* x_ref) = Some(x) {
} It's more consistent with Rust syntax. In my opinion, getting a reference to a value should be an opposite behavior in pattern match, which is dereference. Maybe if let Some(deref x_ref) = Some(x) {
} |
e.g. in methods I can use Looks like nobody gave an answer regarding
So, technically |
In other languages
&
is called a reference and borrowing looks very much like references, so to me&
andref
seem 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?The text was updated successfully, but these errors were encountered: