std_ext/
str.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pub trait StrExt {
    fn into_option(self) -> Option<String>;
}

impl StrExt for &str {
    fn into_option(self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(self.to_string())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_option_string() {
        let s = "hello".to_string();
        assert_eq!(s.into_option().as_deref(), Some("hello"));
    }
}