std_ext/
str.rs

1pub trait StrExt {
2    fn into_option(self) -> Option<String>;
3}
4
5impl StrExt for &str {
6    fn into_option(self) -> Option<String> {
7        if self.is_empty() {
8            None
9        } else {
10            Some(self.to_string())
11        }
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn test_option_string() {
21        let s = "hello".to_string();
22        assert_eq!(s.into_option().as_deref(), Some("hello"));
23    }
24}