I've tried specifying the type in two different places, but the compiler still throws an error.
use serde::Serialize;
// 1: try to set default type.
struct Student<T = Option<String>> {
age: i32,
data: T
}
impl<T> Student<T> where T: Serialize {
fn new(age: i32) -> Self {
Student {
age,
data: <Option<String>>::None // 2. try to set default type
}
}
// fn new_with_data(age: i32, data: T) -> Self {
// Student {
// age,
// data
// }
// }
}
fn main() {
let stu1 = Student::new(123);
//let stu2 = Student::new_with_data(123, <Option<()>>::None);
}
got error:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:13:19
|
8 | impl<T> Student<T> where T: Serialize {
| - this type parameter
...
13 | data: <Option<String>>::None
| ^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found enum `std::option::Option`
|
= note: expected type parameter `T`
found enum `std::option::Option<std::string::String>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
the play link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=749e31fae6091226b99e233a016ca0c8