I have a generic enum:
enum Holder<T> {
None,
One(T),
Two(T, T),
}
and I have placed two strings in it:
let two_strings = Holder::Two(String::from("abc"), String::from("abc"));
I want to write a function which takes the enum, checks if it is of type String
and prints it:
fn print_name_from_enum<T>(hold: T) {
match hold {
Holder::Two(a, b) => println!("Your Name is {} {}", a, b),
}
}
The compiler complains because I do not specify the type of holder
:
error[E0308]: mismatched types
--> src/lib.rs:10:9
|
10 | Holder::Two(a, b) => println!("Your Name is {} {}", a, b),
| ^^^^^^^^^^^^^^^^^ expected type parameter, found enum `Holder`
|
= note: expected type `T`
found type `Holder<_>`
but if I give the type:
match hold {
Holder<String>::Two(a, b) => {
println!("Your Name is {} {}", a, b)
}
}
the compiler complains as well:
error: expected one of `=>`, `@`, `if`, or `|`, found `<`
--> src/lib.rs:10:15
|
10 | Holder<String>::Two(a, b) => {
| ^ expected one of `=>`, `@`, `if`, or `|` here
How do I solve this issue? What am I doing wrong here?
Solution
Thanks to @mcarton I found the mistake. The way it should be is
fn print_name_from_enum<T: ToString>(hold: Holder<T>) {
match hold {
Holder::Two(a, b) => {
println!("Your Name is {first} {second}", first = a.to_string(),
second = b.to_string())
}
_ => {
println!("ooPPs.")
}
}
}
I need to add the enum type as an argument, and for T it has to be add that its function to_string() is implemented.