I am trying to make the return type of a function more
general. Currently, I've had to give it a very specific type which
means I am making some unnecessary type conversions.
While playing around I think maybe the solution is to use Into
; I
could have the function return an Into
type which would allow me to
convert it but only if necessary.
So I have tried this:
pub fn f() -> String {
"hello".to_string()
}
pub fn g() -> impl Into<String> {
"hello".to_string()
}
pub fn h<S:Into<String>>() -> S {
"hello".to_string()
}
In the first function, we have a concrete type. In the second, we use
impl and this works and compiles. In the last case, I return an Into
type. This fails, though because the return statement returns a
String
. But String
implements Into<String>
I though, because all
things implement Into<Themselves>
.
What have I misunderstood?