我嘗試在兩個不同的地方指定型別,但編譯器仍然拋出錯誤。
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);
}
得到錯誤:
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
播放鏈接:https ://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=749e31fae6091226b99e233a016ca0c8
uj5u.com熱心網友回復:
問題是當您設定默認型別時,該impl塊不需要這T是默認型別。<Option<String>>::None僅當型別為 時才為有效值Option<String>,但這并不要求必須如此。
/// `T` is unconstrained and may or may not be `Option<T>`
impl<T> Student<T> {}
/// This implements student for only one specific generic type (`Option<String>`). This
/// functionality will not be available for other generic types.
impl Student<Option<String>> {}
/// Here we don't include the generic so it implements for only the default generic. This is
/// equivalent to the previous version, but the generic is assumed to be the default value.
impl Student {}
我懷疑您想要做的是使資料通用。我們沒有將其Option設為通用,而是將其保存的資料設為通用。我們仍然可以設定默認值而不會遇到這個問題。
struct Student<T = String> {
age: i32,
/// Some optional generic data
data: Option<T>
}
impl<T> Student<T> where T: Serialize {
fn new(age: i32) -> Self {
Student {
age,
data: None,
}
}
fn new_with_data(age: i32, data: T) -> Self {
Student {
age,
data: Some(data),
}
}
}
另一種選擇是保持型別相同,但將其分成impl兩部分。這樣,new_with_data如果資料不是,我們可以要求使用 ,但在某些情況下Option仍然允許使用。new
struct Student<T = Option<String>> {
age: i32,
data: T
}
impl<T> Student<Option<T>> where T: Serialize {
fn new(age: i32) -> Self {
Student {
age,
data: None,
}
}
}
impl<T> Student<T> where T: Serialize {
fn new_with_data(age: i32, data: T) -> Self {
Student { age, data }
}
}
uj5u.com熱心網友回復:
根據你的方法,我做了一點修改,就實作了我想要的。
use serde::Serialize;
struct Student<T = Option<String>> {
age: i32,
/// Some optional generic data
data: T
}
impl Student<Option<String>> {
fn new(age: i32) -> Self {
Student {
age,
data: None,
}
}
}
impl<T> Student<T> where T: Serialize {
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, 2222);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510471.html
標籤:仿制药锈结构类型默认
