Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ fn main() {
// that their object is still alive, we need to call upgrade() on them
// to turn them into a strong reference. This returns an Option, which
// contains a reference to our object if it still exists.
let gadget = gadget_opt.upgrade().unwrap();
println!("Gadget {} owned by {}", gadget.id, gadget.owner.name);
gadget_opt.upgrade().map(|gadget| {
println!("Gadget {} owned by {}", gadget.id, gadget.owner.name);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this example is showing how it is known for a fact that gadget_opt, while a weak pointer, is known to have at least one strong pointer floating around. I'm also not sure that map is the best way to handle options, but rather a match with a None case instead. In general map is used to produce Option<U> from Option<T>, but in this case it's being used in a more iterative context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough! It was suggested on reddit that unwrap should be avoided in general, and that using map was preferable. I'm happy to just close this to be honest.

}

// At the end of the method, gadget_owner, gadget1 and gadget2 get
Expand Down