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?
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.
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”…