@@ -200,6 +200,73 @@ TryPropagationExpression -> Expression `?`
200
200
r[ expr.try.intro]
201
201
The try propagation operator (` ? ` ) unwraps valid values or returns erroneous values, propagating them to the calling function.
202
202
203
+ > [ !EXAMPLE]
204
+ > ``` rust
205
+ > # use std :: num :: ParseIntError ;
206
+ > fn try_to_parse () -> Result <i32 , ParseIntError > {
207
+ > let x : i32 = " 123" . parse ()? ; // `x` is `123`.
208
+ > let y : i32 = " 24a" . parse ()? ; // Returns an `Err()` immediately.
209
+ > Ok (x + y ) // Doesn't run.
210
+ > }
211
+ >
212
+ > let res = try_to_parse ();
213
+ > println! (" {res:?}" );
214
+ > # assert! (res . is_err ())
215
+ > ```
216
+ >
217
+ > ```rust
218
+ > fn try_option_some () -> Option <u8 > {
219
+ > let val = Some (1 )? ;
220
+ > Some (val )
221
+ > }
222
+ > assert_eq! (try_option_some (), Some (1 ));
223
+ >
224
+ > fn try_option_none () -> Option <u8 > {
225
+ > let val = None ? ;
226
+ > Some (val )
227
+ > }
228
+ > assert_eq! (try_option_none (), None );
229
+ > ```
230
+ >
231
+ > ```rust
232
+ > use std :: ops :: ControlFlow ;
233
+ >
234
+ > pub struct TreeNode <T > {
235
+ > value : T ,
236
+ > left : Option <Box <TreeNode <T >>>,
237
+ > right : Option <Box <TreeNode <T >>>,
238
+ > }
239
+ >
240
+ > impl <T > TreeNode <T > {
241
+ > pub fn traverse_inorder <B >(& self , f : & mut impl FnMut (& T ) -> ControlFlow <B >) -> ControlFlow <B > {
242
+ > if let Some (left ) = & self . left {
243
+ > left . traverse_inorder (f )? ;
244
+ > }
245
+ > f (& self . value)? ;
246
+ > if let Some (right ) = & self . right {
247
+ > right . traverse_inorder (f )? ;
248
+ > }
249
+ > ControlFlow :: Continue (())
250
+ > }
251
+ > }
252
+ > #
253
+ > # fn main () {
254
+ > # let n = TreeNode {
255
+ > # value : 1 ,
256
+ > # left : Some (Box :: new (TreeNode {value : 2 , left : None , right : None })),
257
+ > # right : None ,
258
+ > # };
259
+ > # let v = n . traverse_inorder (& mut | t | {
260
+ > # if * t == 2 {
261
+ > # ControlFlow :: Break (" found" )
262
+ > # } else {
263
+ > # ControlFlow :: Continue (())
264
+ > # }
265
+ > # });
266
+ > # assert_eq! (v , ControlFlow :: Break (" found" ));
267
+ > # }
268
+ > ```
269
+
203
270
> [! NOTE ]
204
271
> The try propagation operator is sometimes called * the question mark operator * , * the `? ` operator * , or * the try operator * .
205
272
@@ -215,19 +282,6 @@ If the value is `Err(e)`, then it will return `Err(From::from(e))` from the encl
215
282
r [expr . try . result- ok ]
216
283
If applied to `Ok (x )`, then it will unwrap the value to evaluate to `x `.
217
284
218
- ``` rust
219
- # use std :: num :: ParseIntError ;
220
- fn try_to_parse () -> Result <i32 , ParseIntError > {
221
- let x : i32 = " 123" . parse ()? ; // x = 123
222
- let y : i32 = " 24a" . parse ()? ; // returns an Err() immediately
223
- Ok (x + y ) // Doesn't run.
224
- }
225
-
226
- let res = try_to_parse ();
227
- println! (" {:?}" , res );
228
- # assert! (res . is_err ())
229
- ```
230
-
231
285
r [expr . try . behavior- std - option ]
232
286
When applied to values of the `Option <T >` type , it propagates `None `s .
233
287
@@ -237,20 +291,6 @@ If the value is `None`, then it will return `None`.
237
291
r [expr . try . result- some ]
238
292
If applied to `Some (x )`, then it will unwrap the value to evaluate to `x `.
239
293
240
- ``` rust
241
- fn try_option_some () -> Option <u8 > {
242
- let val = Some (1 )? ;
243
- Some (val )
244
- }
245
- assert_eq! (try_option_some (), Some (1 ));
246
-
247
- fn try_option_none () -> Option <u8 > {
248
- let val = None ? ;
249
- Some (val )
250
- }
251
- assert_eq! (try_option_none (), None );
252
- ```
253
-
254
294
r [expr . try . trait ]
255
295
`? ` cannot be overloaded .
256
296
0 commit comments