Commit 1d0ae3d
committed
core: Implement From<T> for Option<T>
This allows improved ergonomics for functions that have optional
parameters: instead of taking `Option<T>`, they can have a type
parameter bounded by `Into<Option<T>>`. That way, a value of type `T`
can be passed directly without being wrapped by `Some`.
As an example, a function
fn foo<T>(required: i32, optional: T) -> i32
where T: Into<Option<i32>>
{
required + optional.into().unwrap_or(0)
}
can be called as `foo(2, None)` or as `foo(2, 3)`.
Refs rust-lang/rfcs#14021 parent f01bb5e commit 1d0ae3d
2 files changed
+34
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
| |||
710 | 711 | | |
711 | 712 | | |
712 | 713 | | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
713 | 722 | | |
714 | 723 | | |
715 | 724 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
0 commit comments