Skip to content

Commit c014c1a

Browse files
authored
Merge pull request #285 from lly835/refactor/simplify-utils
refactor: 使用惯用 Rust 写法简化 Option 工具函数
2 parents 098703e + c307763 commit c014c1a

1 file changed

Lines changed: 2 additions & 8 deletions

File tree

src/utils.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ pub fn select_option_by_clone<T>(a: &Option<T>, b: &Option<T>) -> Option<T>
1919
where
2020
T: Clone,
2121
{
22-
match a {
23-
Some(_a) => Some(_a.clone()),
24-
None => b.clone(),
25-
}
22+
a.as_ref().cloned().or_else(|| b.clone())
2623
}
2724

2825
pub fn option_owned_by_clone<T>(a: Option<&T>) -> Option<T>
@@ -33,10 +30,7 @@ where
3330
}
3431

3532
pub fn merge_option<T>(a: Option<T>, b: Option<T>) -> Option<T> {
36-
match a {
37-
Some(v) => Some(v),
38-
None => b,
39-
}
33+
a.or(b)
4034
}
4135

4236
pub fn get_bool_from_string(s: &Option<String>, default: bool) -> bool {

0 commit comments

Comments
 (0)