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