about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-13 06:44:15 +0100
committerGitHub <noreply@github.com>2022-02-13 06:44:15 +0100
commit783b56ba68c5737d9593c6e9bb964b44b6889ddc (patch)
tree6ee858a418d57f41fd4f6f3629dd04abcd69b075 /compiler/rustc_codegen_llvm/src
parentaff74a16971237f45182843388ac662c1822f960 (diff)
parentf6f93fd7ba348a4e0e2e9ca7af597d117d6337a1 (diff)
downloadrust-783b56ba68c5737d9593c6e9bb964b44b6889ddc.tar.gz
rust-783b56ba68c5737d9593c6e9bb964b44b6889ddc.zip
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.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
0 files changed, 0 insertions, 0 deletions