The location of the error can be confusing, the problem here is in the collect method. It is common to any FromIterator , in which case the compilerโs cat prints the type (which compiler sees, you need some type X that implements FromIterator , which has the as_slice method that creates & str).
The second problem with this code is that you are trying to return a link to a local value (the result of the collection) from the matching operator. You cannot do this because this value has the lifetime of this block and is freed after that, so the returned one will be invalid.
One working solution (but it requires converting & str to String):
fn main () { f("hello", true); } fn f(str: &str, sen: bool) { let s: String = match sen { false => str.to_string(), true => str.chars().map(|x| x.to_lowercase()).collect() }; println!("{}", s); }
I donโt know what you are trying to achieve at the end, but look at MaybeOwned if this is a function that returns a slice ( &str ) or String .
source share