rust/library/core
Matthias Krüger 783b56ba68
Rollup merge of #93851 - cyqsimon:option-examples, r=scottmcm
More practical examples for `Option::and_then` & `Result::and_then`

To be blatantly honest, I think the current example given for `Option::and_then` is objectively terrible. (No offence to whoever wrote them initially.)

```rust
fn sq(x: u32) -> Option<u32> { Some(x * x) }
fn nope(_: u32) -> Option<u32> { None }

assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16));
assert_eq!(Some(2).and_then(sq).and_then(nope), None);
assert_eq!(Some(2).and_then(nope).and_then(sq), None);
assert_eq!(None.and_then(sq).and_then(sq), None);
```

Current example:
 - does not demonstrate that `and_then` converts `Option<T>` to `Option<U>`
 - is far removed from any realistic code
 - generally just causes more confusion than it helps

So I replaced them with two blocks:
 - the first one shows basic usage (including the type conversion)
 - the second one shows an example of typical usage

Same thing with `Result::and_then`.

Hopefully this helps with clarity.
2022-02-13 06:44:15 +01:00
..
benches Respond to review feedback, and improve implementation somewhat 2022-02-05 11:15:18 -08:00
primitive_docs Add primitive documentation to libcore 2021-09-12 02:23:08 +00:00
src Rollup merge of #93851 - cyqsimon:option-examples, r=scottmcm 2022-02-13 06:44:15 +01:00
tests Stabilize cfg_target_has_atomic 2022-02-09 18:45:44 +00:00
Cargo.toml Build libcore as 2021 in a few more places 2022-02-06 15:41:01 -08:00