Skip to content

Commit 8334b19

Browse files
ehusstraviscross
authored andcommitted
Move try examples to an example block
This moves the examples to the intro. It also adds a ControlFlow example. The intent here is to prepare for changing the rest of the text, and the examples won't quite fit like the used to. I also prefer the idea of having an example near every intro section. Edited-by: TC
1 parent 66308ef commit 8334b19

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

src/expressions/operator-expr.md

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,73 @@ TryPropagationExpression -> Expression `?`
200200
r[expr.try.intro]
201201
The try propagation operator (`?`) unwraps valid values or returns erroneous values, propagating them to the calling function.
202202

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+
203270
> [!NOTE]
204271
> The try propagation operator is sometimes called *the question mark operator*, *the `?` operator*, or *the try operator*.
205272
@@ -215,19 +282,6 @@ If the value is `Err(e)`, then it will return `Err(From::from(e))` from the encl
215282
r[expr.try.result-ok]
216283
If applied to `Ok(x)`, then it will unwrap the value to evaluate to `x`.
217284
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-
231285
r[expr.try.behavior-std-option]
232286
When applied to values of the `Option<T>` type, it propagates `None`s.
233287
@@ -237,20 +291,6 @@ If the value is `None`, then it will return `None`.
237291
r[expr.try.result-some]
238292
If applied to `Some(x)`, then it will unwrap the value to evaluate to `x`.
239293
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-
254294
r[expr.try.trait]
255295
`?` cannot be overloaded.
256296

0 commit comments

Comments
 (0)