Why are type parameters not allowed for this type?

Trying to compile the following,

fn do_it() -> Result<i32, u32> { Result::<i32, u32>::Ok(3) } fn main() { println!("{}", do_it()); } 

leads to:

 ./result_test.rs:2:14: 2:17 error: type parameters are not allowed on this type [E0109] ./result_test.rs:2 Result::<i32, u32>::Ok(3) ^~~ 

Why are type parameters not allowed for this type?

This is a minimal example, my real example is a macro trying to return the following:

 match $reader.$read_func() { Ok(n) => Result::<$read_type, LocalReadError>::Ok(n), Err(err) => Result::<$read_type, LocalReadError>::Err( LocalReadError::from(err) ), } 

$read_func is a function, $read_type is the return type of this function. (If I had a programmatic way to get this, I would do it, I don't know how, so this is an argument ...); as-is, I get the above error. If I remove the general parameter specification, enter inteference so that it cannot determine the type. (Because it ends with Result<_, LocalReadError> in one match branch and Result<$read_type, _> in another? I'm not sure. He says:

 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282] match $reader.$read_func() { ^~~~~~~~~~~~ 

)

Note. The question of why type parameters are not allowed is given below. It turns out that this is not the reason that "it is not possible to determine sufficient type information." ( read_func is a function, in my case I pass a template function, but I forget the arg template, which cannot be displayed.)

+7
macros generics type-inference rust
source share
1 answer

this is actually inconsistency with the enumerations that was discussed , but not considered important enough to block 1.0.

The working syntax for specifying types is Result::Ok::<i32, u32>(3) .

An enumerator works like something between a type (which will be with the syntax you tried to write) and a namespace (and namespaces do not accept type parameters).

To demonstrate how enums are similar to namespaces, you can write:

 use std::result::Result::*; fn main() { println!("{:?}", Ok::<i32, u32>(3)); } 

This namespacing attribute is a desirable property of enumerations, but parameters of type move, where it was intuitively assumed that they would make this type of code very inconvenient for writing.

+9
source share

All Articles