Commit 783b56b
authored
Rollup merge of rust-lang#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.2 files changed
+37
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1207 | 1207 | | |
1208 | 1208 | | |
1209 | 1209 | | |
1210 | | - | |
1211 | | - | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
1212 | 1226 | | |
1213 | | - | |
1214 | | - | |
1215 | | - | |
1216 | | - | |
| 1227 | + | |
| 1228 | + | |
1217 | 1229 | | |
1218 | 1230 | | |
1219 | 1231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1281 | 1281 | | |
1282 | 1282 | | |
1283 | 1283 | | |
1284 | | - | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
1285 | 1288 | | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
1286 | 1292 | | |
1287 | | - | |
1288 | | - | |
1289 | 1293 | | |
1290 | | - | |
1291 | | - | |
1292 | | - | |
1293 | | - | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
1294 | 1306 | | |
1295 | 1307 | | |
1296 | 1308 | | |
| |||
0 commit comments