-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Make Option.unwrap documentation more precise #23713
Comments
This example will not compile, because #[derive(PartialEq, Debug)]
struct Foo;
fn main() {
let x = Some(Foo);
assert_eq!(x.unwrap(), Foo);
assert_eq!(x.is_some(), true);
} The error is:
The documentation could be updated to mention moving, but this is implicit in the method's |
@apasel422 thanks a lot for your explanation! Still, I don't think I grasped how the struct Item {
id: isize
}
struct EntryMutBorrow<'a> {
body: Option<&'a mut Item>
}
fn main() {
let mut item = Item { id: 1 };
let entry_ref = &mut EntryMutBorrow { body: Some(&mut item) };
// ATTEMPT FAILED:
// The following does not work as moving a value from a borrow
// is not possible and `entry_ref.body` a borrow.
//
// error: cannot move out of borrowed content
//
// let item_mut_borrow: &mut Item = entry_ref.body.unwrap();
// ATTEMPT WORKS:
// The following works, but it is not clear to me why: Why is moving the
// value from an `Option<&mut &mut Item>` type not an problem but having
// a single borrow as `Option<&mut Item>` is not enough?
let item_as_ref: Option<&mut &mut Item> = entry_ref.body.as_mut();
let item_mut_borrow: &mut &mut Item = item_as_ref.unwrap();
// Update the id on the entry.item.id - the println! confirms the change succeeded.
item_mut_borrow.id = 2;
println!("entry.body.id={}", item_mut_borrow.id);
// PROGRAM OUTPUT:
// entry.body.id=2
} I understand that moving a value from a borrowed object is not possible and that's why the first attempt fails. However, I don't see why the second attempt works. In a sense, the new type after the call to Is there a special rule to the |
Basically, in order to call The code under "ATTEMPT FAILED" doesn't compile because For example, the following code does compile: struct Item {
id: isize
}
struct EntryMutBorrow<'a> {
body: Option<&'a mut Item>
}
fn main() {
let mut item = Item { id: 1 };
let entry_ref = EntryMutBorrow { body: Some(&mut item) };
let item_mut_borrow: &mut Item = entry_ref.body.unwrap();
item_mut_borrow.id = 2;
println!("entry.body.id={}", item_mut_borrow.id);
// PROGRAM OUTPUT:
// entry.body.id=2
} |
Thanks again @apasel422! The point that you need an owned What I don't see at this point is why fn test_05() {
let mut item = Item { id: 1 };
let entry_ref = &mut EntryMutBorrow { body: Some(&mut item) };
// The following type checks, so I guess the type of `entry_ref.body`
// is `Option<&mut Item>` here. The program is stil rejected because
//
// error: cannot move out of borrowed content
//
let body_ref: Option<&mut Item> = entry_ref.body;
} It seems that Any comments is highly appreciated! PS: @apasel422, I hope digging deeper on this issue is okay for you? From your comments so far I think I can fix the |
In that example, the type of |
Ahh, I think now I get it - thanks a lot @apasel422. Are these the (hidden) steps the compiler is doing when executing
I've got to say: I love and be impressed what the rust compiler is doing underneath here (once you understand it :) )! @apasel422, thank you sooo much for these helpful and very educational comments!!! |
So, given all this: is there anything actionable we can do to improve these docs? What would you suggest, @jviereck ? |
@steveklabnik, how do you feel about this:
|
I like it! would you like to submit a PR or should I take care of it?
I'm not sure. |
I don't think we need to add anything about However, |
Reading the current documentation about
Option<T>.unwrap()
athttp://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap
it is not clear to me what the effect of
unwrap()
is. I was supposing calling this function returns the value stored in the Option type and replaces it by anNone
value. However, this seems not to be the case as the second assertion in the following example program passes fine:(You can run the program online at http://goo.gl/F9OnFk).
In addition, the current documentation says:
Does this means the value contained in the
Some(T)
is moved out or cloned or is a borrow taken? Also,T
is a type and I don't think that the function really returns a type, but a value of typeT
. Therefore, should the quoted sentence maybe reworded towards something along the lines of:Please let me know what you think about these points. I am more than happy to provide a PR to improve the documentation, but I don't know all the answers to the questions I have raised above and I am also not sure if my claims are correct :/
The text was updated successfully, but these errors were encountered: