Skip to content

Commit 7e7f89a

Browse files
committed
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.
1 parent b677de4 commit 7e7f89a

File tree

1 file changed

+69
-27
lines changed

1 file changed

+69
-27
lines changed

src/expressions/operator-expr.md

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,75 @@ 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+
> 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+
203272
> [!NOTE]
204273
> The try propagation operator is sometimes called *the question mark operator*, *the `?` operator*, or *the try operator*.
205274
@@ -215,19 +284,6 @@ If the value is `Err(e)`, then it will return `Err(From::from(e))` from the encl
215284
r[expr.try.result-ok]
216285
If applied to `Ok(x)`, then it will unwrap the value to evaluate to `x`.
217286
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-
231287
r[expr.try.behavior-std-option]
232288
When applied to values of the `Option<T>` type, it propagates `None`s.
233289
@@ -237,20 +293,6 @@ If the value is `None`, then it will return `None`.
237293
r[expr.try.result-some]
238294
If applied to `Some(x)`, then it will unwrap the value to evaluate to `x`.
239295
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-
254296
r[expr.try.trait]
255297
`?` cannot be overloaded.
256298

0 commit comments

Comments
 (0)