Pre-RFC: None if string is empty


#1

It’s simple, I’d love a function that returns takes a String and returns an Option<String>.

Example implementation:

pub fn none_if_empty(self) -> Option<String> {
    if self.is_empty() { None } else { Some(self) }
}

You’re probably asking: “But whyyyyyyyyyyy?”

Honestly I don’t really have a better answer than “Why not?”.
It’s useful when chaining stuff, for example:

if let Some(num) = some_string.iter()
                         .filter(|c| c.is_digit())
                         .collect::<String>()
                         .none_if_empty()
                         .map(|s| s.parse()) {
    println!("Number: {}", num);
}

Problems: What if we then want more functions like this? It could add an unecessary complexity to the string type. Would it be better to have something like .none_if(String::is_empty)? Should it be specific to strings?


#2

This can be easily implemented in a crate if needed. Also, it seems like it should be also added for a &str type, not just owned string.

#[macro_use]
extern crate extension_trait;

extension_trait! { pub NoneIfEmptyExt for String {
    fn none_if_empty(self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(self)
        }
    }
} }

That said, I like the concept of none_if function, often enough I actually wrote code that returned None if a condition is met. For example many C functions return special values when there is no result which could be abstracted away with such a function.


#3

What about something like

trait IntoOption where Self: Sized {
    fn none_if<F>(self, callback: F) -> Option<Self>
        where F: Fn(&Self) -> bool
    {
        if callback(&self) { None } else { Some(self) }
    }
}
impl<T> IntoOption for T {}

?
That should implement it for every single type.

Playground link


#4

I think on nightly this can be phrased as Some(the_string).filter(|x| !x.is_empty()).

But overall, I’m not a big fan of std methods for this kind of thing. It tends to lead to things like .Net’s String.IsNullOrWhitespace that usually just make me think “why didn’t you have an option to begin with”…


#5

Great, even better. Thanks! :slight_smile: