@@ -48,21 +48,41 @@ if let Some(max) = max {
48
48
## Null-conditional operators
49
49
50
50
The null-conditional operators (` ?. ` and ` ?[] ` ) make dealing with ` null ` in C#
51
- more ergonomic. In Rust, they are best replaced by using the [ ` map ` ] [ optmap ]
52
- method. The following snippets show the correspondence:
51
+ more ergonomic. In Rust, they are best replaced by using either the [ ` map ` ] [ optmap ]
52
+ method or the [ ` and_then ` ] [ opt_and_then ] method, depending on the nesting of the ` Option ` .
53
+ The following snippets show the correspondence:
53
54
54
55
``` csharp
55
56
string ? some = " Hello, World!" ;
56
57
string ? none = null ;
57
- Console .WriteLine (some ? .Length ); // 13
58
+ Console .WriteLine (some ? .Length ); // Hello, World!
58
59
Console .WriteLine (none ? .Length ); // (blank)
60
+
61
+ record Name (string FirstName , string LastName );
62
+ record Person (Name ? Name );
63
+
64
+ Person ? person1 = new Person (new Name (" John" , " Doe" ));
65
+ Console .WriteLine (person1 ? .Name ? .FirstName ); // John
66
+ Person ? person2 = new Person (null );
67
+ Console .WriteLine (person2 ? .Name ? .FirstName ); // (blank)
68
+ Person ? person3 = null ;
69
+ Console .WriteLine (person3 ? .Name ? .FirstName ); // (blank)
59
70
```
60
71
61
72
``` rust
62
73
let some : Option <String > = Some (String :: from (" Hello, World!" ));
63
74
let none : Option <String > = None ;
64
- println! (" {:?}" , some . map (| s | s . len ())); // Some(13 )
75
+ println! (" {:?}" , some . map (| s | s . len ())); // Some("Hello, World!" )
65
76
println! (" {:?}" , none . map (| s | s . len ())); // None
77
+
78
+ struct Name { first_name : String , last_name : String }
79
+ struct Person { name : Option <Name > }
80
+ let person1 : Option <Person > = Some (Person { name : Some (Name { first_name : " John" . into (), last_name : " Doe" . into () }) });
81
+ println! (" {:?}" , person1 . and_then (| p | p . name. map (| name | name . first_name))); // Some("John")
82
+ let person1 : Option <Person > = Some (Person { name : None });
83
+ println! (" {:?}" , person1 . and_then (| p | p . name. map (| name | name . first_name))); // None
84
+ let person1 : Option <Person > = None ;
85
+ println! (" {:?}" , person1 . and_then (| p | p . name. map (| name | name . first_name))); // None
66
86
```
67
87
68
88
## Null-coalescing operator
@@ -98,4 +118,5 @@ there is no need to use a substitute for it.
98
118
99
119
[ option ] : https://doc.rust-lang.org/std/option/enum.Option.html
100
120
[ optmap ] : https://doc.rust-lang.org/std/option/enum.Option.html#method.map
121
+ [ opt_and_then ] : https://doc.rust-lang.org/std/option/enum.Option.html#method.and_then
101
122
[ unwrap-or ] : https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or
0 commit comments