You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ref is used in pattern matching, so its used when you want to borrow
something in, for example, a match statement.
let a = Some("Hello!".to_string());
match a {
Some(ref s) => { //s is an &String here
}
...
}
match a {
Some(s) => {
// s is a String here, and therefore
// is owned by the match
}
...
}
mverleg, 907th, pirogoeth, hbobenicio, sidd-kulk and 7 more
I'm not trying to question Rust design decision here, just understand difference between ref and & — and I'm reporting that the example about references doesn't elaborate on that.
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 &_-ptr or such when I use & instead of ref.
nbro, JesseWright, vijairaj, vermiculus, evgeni-nabokov and 2 more
I'm not trying to question Rust design decision here, just understand
difference between ref and & — and I'm reporting that the example about
references doesn't elaborate on that.
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 &_-ptr or such when I use
& instead of ref.
—
Reply to this email directly or view it on GitHub #390 (comment)
.
gelisamnbro, LINKIWI, xasopheno and SephVelutnbro and lucidBrot
and there's no room in it for ref. If it's like this:
object ---- ref operator ---> pointer
then in my mind it's identical with &, but clearly in Rust there must be a difference.
When I use & where Rust expects ref I get error message that I don't understand:
mismatched types: expected isize, found &_ (expected isize, found &-ptr)
I'm not sure what &_ and &-ptr are, but I'm guessing it means reference to an unknown type. And it's weird, because the type should be known from the context.
niketkumar, codyebberson, discosultan, chpio, nbro and 13 more
So where are you seeing this error? In Rust, you basically have this:
let a = &i;
let r = *a;
// These are exactly like in C (i.e. `&i` gets the address of i,
// `*a` dereferences a)
And in pattern matching, you do this:
let a = Some(1i); // This is how we construct an Option
match a {
Some(x) => println!("{}', x), // This prints '1'
// Notice we "deconstruct" in a match statement the same way we
// normally construct
...
}
What about when we have to get a reference in a match statement where there was
none before? In normal code, getting a reference to a variable is easy.
struct BigFoo {
// Lots of fields
}
fn bar(bf: &BigFoo) {
// Do stuff with bf
}
fn main() {
let bf = BigFoo {
// lots of fields
}
bar(&bf);
}
However, what if you have, say, a match statement?
fn possibly_print(x: &Option<BigFoo>) {
match *x {
// BAD: It's impossible to move out of an &-reference (the Option)
//Some(bf) => println!("{:?}", &s),
// GOOD: Instead, we take a reference into the `Option`s memory
Some(ref bf) => println!("{:?}", *bf),
None => println("No BigFoo!"),
}
}
So I think I'm starting to get why it's illegal syntax:
fnmain(){let x = 123;let&x_ref_1 = &x;letref x_ref_2 = &x;//let & x_ref_3 = x; // same error as when matching &xletref 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);// legallet s2 = Some(ref x);// illegal
Hi-Angel, rosswendt and SoftwareApenbro, rosswendt, 0x8f701 and ebenezerrahul
Thank you very much for bringing this incredibly confusing thing to our
attention; I hadn't even thought about it before you said it :)
On Jan 12, 2015 2:37 PM, "Kornel" notifications@github.com wrote:
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 sidelet 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.
—
Reply to this email directly or view it on GitHub #390 (comment)
.
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.
Activity
strega-nil commentedon Jan 11, 2015
ref
is used in pattern matching, so its used when you want to borrowsomething in, for example, a match statement.
kornelski commentedon Jan 11, 2015
Why can't you use
&
to borrow in the match??
e.g. in methods I can use
&self
instead ofref self
.strega-nil commentedon Jan 11, 2015
That, I don't know. It's not really a decision I agree with. I like that
style better, personally.
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."
"The lone and level sands stretch far and away"
kornelski commentedon Jan 11, 2015
I'm not trying to question Rust design decision here, just understand difference between
ref
and&
— and I'm reporting that the example about references doesn't elaborate on that.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
&_-ptr
or such when I use&
instead ofref
.strega-nil commentedon Jan 11, 2015
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:
kornelski commentedon Jan 12, 2015
Sorry, I don't understand your last answer.
&x
is dereferencing a pointer? I thought*x
is dereferencing.Perhaps I'm asking stupid questions, but the mental model I have is from C:
and there's no room in it for
ref
. If it's like this:then in my mind it's identical with
&
, but clearly in Rust there must be a difference.When I use
&
where Rust expectsref
I get error message that I don't understand:I'm not sure what
&_
and&-ptr
are, but I'm guessing it means reference to an unknown type. And it's weird, because the type should be known from the context.strega-nil commentedon Jan 12, 2015
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
none before? In normal code, getting a reference to a variable is easy.
However, what if you have, say, a match statement?
(Note: this is taken directly from the rust docs book)
Does that make sense?
kornelski commentedon Jan 12, 2015
The error I was getting was from
So I think I'm starting to get why it's illegal syntax:
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:
strega-nil commentedon Jan 12, 2015
There are two things going on here:
ref
is only useful for match statements and the like; it's literally only forkornelski commentedon Jan 12, 2015
Oh, that's interesting! Are these two statements equivalent?
strega-nil commentedon Jan 12, 2015
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."
"The lone and level sands stretch far and away"
kornelski commentedon Jan 12, 2015
Ok, so now it makes sense to me! Thanks!
I suggest adding such simple example to the site:
The full example is useful as well, but with destructuring at the same time there's much more going on.
strega-nil commentedon Jan 12, 2015
Thank you very much for bringing this incredibly confusing thing to our
attention; I hadn't even thought about it before you said it :)
On Jan 12, 2015 2:37 PM, "Kornel" notifications@github.com wrote:
mdinger commentedon Jan 20, 2015
@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.
flow control
group. Addif let
explanations. #42119 remaining items