Commit 6c4fc2f
Merge #593
593: EitherOrBoth: Add or and or_else methods to simplify getting default values r=phimuemue a=carl-anders
## Introducing `or` and `or_else` methods to `EitherOrBoth`
This feature reduces the amount of code required to get specific default values when using EitherOrBoth.
An example of the current way of using `zip_longest` with custom default values:
```rust
let a = (0..=4).into_iter();
let b = (0..=2).into_iter();
let c = a.zip_longest(b).map(|e| match e {
EitherOrBoth::Both(l, r) => (l, r),
EitherOrBoth::Left(l) => (l, 2),
EitherOrBoth::Right(r) => (4, r),
});
// c will now contain an iterator with (0,0), (1,1), (2,2), (3,2), (4,2).
```
An example with the proposed `or` method:
```rust
let a = (0..=4).into_iter();
let b = (0..=2).into_iter();
let c = a.zip_longest(b).map(|e| e.or(4, 2));
// c will now contain an iterator with (0,0), (1,1), (2,2), (3,2), (4,2).
```
I have also included the `or_else` method which does the same but with closures.
## Contribute questions
> Include tests for your new feature, preferably a QuickCheck test
There are no tests for the other `EitherOrBoth` methods, so I was unsure how to place them. However, I have added **DocTest**'s to both methods. These examples allows for easier to understand documentation, and also tests the validity of the methods.
> For new features, please first consider filing a PR to rust-lang/rust
The EitherOrBoth struct does not exist in rust std library.
## Concerns
The naming is slightly inconsistent when compared to rust's `std::Option`, considering the naming from there the added methods should be named `unwrap_or` and `unwrap_or_else`.
However, this would then become inconsistent with the existing method `EitherOrBoth::or_default`. Which is why I went with the chosen names.
I can change the method names if needed, but then we should consider changing `or_default` too.
## P.S.
The `CHANGELOG.md` file has the text `Add EitherOrBoth::or_default (#583)`. This number is wrong, it should be #538.
Co-authored-by: Carl Andersson <carl@carlandersson.net>1 file changed
+49
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
167 | 194 | | |
168 | 195 | | |
169 | 196 | | |
| |||
178 | 205 | | |
179 | 206 | | |
180 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
181 | 230 | | |
182 | 231 | | |
183 | 232 | | |
| |||
0 commit comments