Returning an Err from a generic function


#1

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.


#2

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


#3

Sigh. I’m stupid. It works: https://play.rust-lang.org/?gist=88fb478aeb3295d7a5c2257574018813&version=nightly&backtrace=0
(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.