Returning an Err from a generic function

I'm trying to implement a simple function that returns a generic error message.

fn unimplemented<T>() -> Result<T, String> {
    let res: Result<T, String> = Err("Not implemented yet!".to_string());
    res
}

(I'm aware of the unimplemented! macro, but I don't want a panic, just a Result)

The compiler isn't being very helpful:

error[E0282]: unable to infer enough type information about `_`
   --> src\main.rs:214:9
    |
214 |         Err("Unimplemented");
    |         ^^^ cannot infer type for `_`
    |
    = note: type annotations or generic parameter binding required

I cannot for the life of me find any information on this error message that is relevant to my problem, nor on type annotations in Rust in general.

Can you post a condensed example using http://play.rust-lang.org ?

Sigh. I'm stupid. It works: Rust Playground
(The error was elsewhere in my code, in very similar, yet wrong version of the code)
Sorry for the noise. I couldn't find a way to delete this topic.

1 Like