diff options
| author | Canop <cano.petrole@gmail.com> | 2020-10-01 11:13:38 +0200 |
|---|---|---|
| committer | Canop <cano.petrole@gmail.com> | 2020-10-23 11:41:19 +0200 |
| commit | 9b90e1762e6cb21baa504a27530b9aa404fbe3ac (patch) | |
| tree | 564fe16c2a195a1d5b2a57dd05d6da597cb7cafb /src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha1.rs | |
| parent | a9cd294cf2775441e713c7ee2918b728733b99f5 (diff) | |
| download | rust-9b90e1762e6cb21baa504a27530b9aa404fbe3ac.tar.gz rust-9b90e1762e6cb21baa504a27530b9aa404fbe3ac.zip | |
add `insert` and `insert_with` to `Option`
This removes a cause of `unwrap` and code complexity.
This allows replacing
```
option_value = Some(build());
option_value.as_mut().unwrap()
```
with
```
option_value.insert(build())
```
or
```
option_value.insert_with(build)
```
It's also useful in contexts not requiring the mutability of the reference.
Here's a typical cache example:
```
let checked_cache = cache.as_ref().filter(|e| e.is_valid());
let content = match checked_cache {
Some(e) => &e.content,
None => {
cache = Some(compute_cache_entry());
// unwrap is OK because we just filled the option
&cache.as_ref().unwrap().content
}
};
```
It can be changed into
```
let checked_cache = cache.as_ref().filter(|e| e.is_valid());
let content = match checked_cache {
Some(e) => &e.content,
None => &cache.insert_with(compute_cache_entry).content,
};
```
Diffstat (limited to 'src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha1.rs')
0 files changed, 0 insertions, 0 deletions
