I want to write a function that would reverse a string and return a reference to the returned string. I thought I would be able to specify that the returned reference has the same lifetime as the provided reference.
fn reverse_string<'a>(input: &'a str) -> &'a str {
let res: &'a str = input.chars().rev().collect().as_slice();
res
}
This was my attempt, and I receive the error:
error[E0282]: type annotations needed
--> src/lib.rs:2:24
|
2 | let res: &'a str = input.chars().rev().collect().as_slice();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `B`
|
= note: type must be known at this point
I thought the type specification would resolve this. Lifetimes are a bit confusing to me, so I was hoping someone may be able to explain how to do this properly, or if I'm completely off base to begin with.