Skip to content

Commit fe848fc

Browse files
committed
rollup merge of rust-lang#16828 : steveklabnik/more_pointer_guide
2 parents c6fd2d3 + 7e4a145 commit fe848fc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/doc/guide-pointers.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,31 @@ This part is coming soon.
729729

730730
This part is coming soon.
731731

732+
# Patterns and `ref`
733+
734+
When you're trying to match something that's stored in a pointer, there may be
735+
a situation where matching directly isn't the best option available. Let's see
736+
how to properly handle this:
737+
738+
```{rust,ignore}
739+
fn possibly_print(x: &Option<String>) {
740+
match *x {
741+
// BAD: cannot move out of a `&`
742+
Some(s) => println!("{}", s)
743+
744+
// GOOD: instead take a reference into the memory of the `Option`
745+
Some(ref s) => println!("{}", *s),
746+
None => {}
747+
}
748+
}
749+
```
750+
751+
The `ref s` here means that `s` will be of type `&String`, rather than type
752+
`String`.
753+
754+
This is important when the type you're trying to get access to has a destructor
755+
and you don't want to move it, you just want a reference to it.
756+
732757
# Cheat Sheet
733758

734759
Here's a quick rundown of Rust's pointer types:

0 commit comments

Comments
 (0)