Hello there!
How to get this code compile?
pub enum Foo<'a, Operation> {
A0 { str: String, data: &'a Operation },
A1 { str: String },
}
fn main() {
let arr= vec![0, 1, 2];
let a0 = Foo::A0{str: "arr".to_owned(), data: &arr};
let a1 = Foo::<'_, ()>::A1{str: "no_arr".to_owned()};
}
Got error:
error[E0109]: lifetime arguments are not allowed for this type
--> main\src\main.rs:76:29
|
76 | let a1 = Foo::<'_, ()>::A1{str: "no_arr".to_owned()};
| ^^ lifetime argument not allowed
Ok, lifetime not allowed:
pub enum Foo<'a, Operation> {
A0 { str: String, data: &'a Operation },
A1 { str: String },
}
fn main() {
let arr= vec![0, 1, 2];
let a0 = Foo::A0{str: "arr".to_owned(), data: &arr};
let a1 = Foo::A1{str: "no_arr".to_owned()};
}
Got error:
error[E0282]: type annotations needed for `Foo<'_, Operation>`
--> main\src\main.rs:76:14
|
76 | let a1 = Foo::A1{str: "no_arr".to_owned()};
| -- ^^^^^^^ cannot infer type for type parameter `Operation` declared on the enum `Foo`
| |
| consider giving `a1` the explicit type `Foo<'_, Operation>`, where the type parameter `Operation` is specified
But if we use tuples everything fine:
pub enum Foo<'a, Operation> {
A0(String, &'a Operation),
A1(String),
}
fn main() {
let arr= vec![0, 1, 2];
let a0 = Foo::A0("arr".to_owned(), &arr);
let a1 = Foo::<'_, ()>::A1("no_arr".to_owned());
}