File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -729,6 +729,31 @@ This part is coming soon.
729
729
730
730
This part is coming soon.
731
731
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
+
732
757
# Cheat Sheet
733
758
734
759
Here's a quick rundown of Rust's pointer types:
You can’t perform that action at this time.
0 commit comments