【发布时间】:2021-08-29 12:19:19
【问题描述】:
我有以下代码尝试应用泛型。
fn main() {
let HALVING2016: When= When {
year: 2016.0,
month: 7.0,
day: 9.0,
};
let HIGH2017: When= When {
year: 2017.0,
month: 12.0,
day: 20.0,
};
let HALVING2020: When= When {
year: 2020.0,
month: 5.0,
day: 18.0,
};
let DAYS_IN_Y: f32 = 365.25;
let DAYS_IN_M: f32 = 30.43757;
let NO_OF_DAYS_FROM_YEARS: f32 = (HIGH2017.year - HALVING2016.year) * DAYS_IN_Y ;
let NO_OF_DAYS_FROM_MONTHS: f32 = (HIGH2017.month - HALVING2016.month) * DAYS_IN_M ; //This number can be negative!
let NO_OF_DAYS_FROM_DAYS: f32 = HIGH2017.day - HALVING2016.day ; //This number can be negative!
let DAYS_ALLTOGETHER: f32 = NO_OF_DAYS_FROM_YEARS + NO_OF_DAYS_FROM_MONTHS + NO_OF_DAYS_FROM_DAYS;
let MONTHS_ALLTOGETHER: f32 = DAYS_ALLTOGETHER / DAYS_IN_M ;
let DAYS_FROM_DM: f32 = DAYS_ALLTOGETHER - DAYS_IN_Y ;
let high2021: When= When {
year: HALVING2020.year + (MONTHS_ALLTOGETHER) / 12.0,
month: DAYS_FROM_DM / DAYS_IN_M + 5.0 ,
day: 20.0 + NO_OF_DAYS_FROM_DAYS ,
};
let low2015: Low = Low {
high: 1128.4,
low: 197.84 ,
percentage: (1128.4 - 197.84 ) / 1128.4,
};
println!("The all-time high for Bitcoin will happen on {:.0}/{:.0}/{:.0} i.e. {:.1} months from its halving in May of 2020.", high2021.day, high2021.month, high2021.year, MONTHS_ALLTOGETHER );
let low2018: Low = Low {
high: 19798.6,
low: 3156.2, //13/12/2018 the same date as XMR
percentage: (19798.6 - 3156.2 ) / 19798.6,
};
let low2022: Low = Low {
high: ( low2018.high / 1128.4 ) * low2018.high , //We do not have the data for the ATH of XMR that corresponds to BTC 1128.4
low: (1.0 - 0.8326284) * ( low2018.high / 1128.4 ) * low2018.high,
percentage: (0.82467216 + 0.8405847 ) / 2.0,
};
println!("The all-time high of ${:.0} for Bitcoin was followed by the drop of {:.1}%.", low2015.high, low2015.percentage * 100.0 );
println!("The last all-time high of {:.0} for Bitcoin was followed by the drop of {:.1}%.", low2018.high, low2018.percentage * 100.0 );
println!("The high of 2017 was {:.2} times higher than the high of 2014. The average of the last two corrections is {:.1}%.\n", low2022.high / low2018.high, low2022.percentage * 100.0 );
println!("Should the high of 2021 be {:.2} times higher than the high of 2017 it would be at the price of ${:.0}.", low2022.high / low2018.high, low2022.high );
println!("Should the correction in the 2022-2023 be that of {:.2}% from the top of {:.0}, the bottom would be at ${:.0}.\n", low2022.percentage * 100.0, low2022.high, low2022.low );
}
struct When{
year: f32,
month: f32,
day: f32,
}
struct Low {
high: f32,
low: f32,
percentage: f32
}
一开始我试过了
通过替换结构When的定义来在结构定义中使用泛型:
struct When<T>{
year: T,
month: T,
day: T,
}
然后,当我运行它时,我会从编译器获取消息:
error[E0107]: missing generics for struct `When`
--> src/main.rs:3:18
|
3 | let HALVING2016: When = When {
| ^^^^ expected 1 type argument
|
note: struct defined here, with 1 type parameter: `T`
--> src/main.rs:86:8
|
86 | struct When<T>{
| ^^^^ -
help: use angle brackets to add missing type argument
|
3 | let HALVING2016: When<T> = When {
按照编译器的建议,我添加了缺少的类型参数 (<T>),因此代码现在看起来像这样:
fn main() {
let HALVING2016: When<T> = When {
year: 2016.0,
month: 7.0,
day: 9.0,
};
let HIGH2017: When<T> = When {
year: 2017.0,
month: 12.0,
day: 20.0,
};
let HALVING2020: When<T> = When {
year: 2020.0,
month: 5.0,
day: 18.0,
};
let DAYS_IN_Y: f32 = 365.25;
let DAYS_IN_M: f32 = 30.43757;
let NO_OF_DAYS_FROM_YEARS: f32 = (HIGH2017.year - HALVING2016.year) * DAYS_IN_Y ;
let NO_OF_DAYS_FROM_MONTHS: f32 = (HIGH2017.month - HALVING2016.month) * DAYS_IN_M ; //This number can be negative!
let NO_OF_DAYS_FROM_DAYS: f32 = HIGH2017.day - HALVING2016.day ; //This number can be negative!
let DAYS_ALLTOGETHER: f32 = NO_OF_DAYS_FROM_YEARS + NO_OF_DAYS_FROM_MONTHS + NO_OF_DAYS_FROM_DAYS;
let MONTHS_ALLTOGETHER: f32 = DAYS_ALLTOGETHER / DAYS_IN_M ;
let DAYS_FROM_DM: f32 = DAYS_ALLTOGETHER - DAYS_IN_Y ;
let high2021: When<T> = When {
year: HALVING2020.year + (MONTHS_ALLTOGETHER) / 12.0,
month: DAYS_FROM_DM / DAYS_IN_M + 5.0 ,
day: 20.0 + NO_OF_DAYS_FROM_DAYS ,
};
let low2015: Low = Low {
high: 1128.4,
low: 197.84 ,
percentage: (1128.4 - 197.84 ) / 1128.4,
};
println!("The all-time high for Bitcoin will happen on {:.0}/{:.0}/{:.0} i.e. {:.1} months from its halving in May of 2020.", high2021.day, high2021.month, high2021.year, MONTHS_ALLTOGETHER );
let low2018: Low = Low {
high: 19798.6,
low: 3156.2, //13/12/2018 the same date as XMR
percentage: (19798.6 - 3156.2 ) / 19798.6,
};
let low2022: Low = Low {
high: ( low2018.high / 1128.4 ) * low2018.high , //We do not have the data for the ATH of XMR that corresponds to BTC 1128.4
low: (1.0 - 0.8326284) * ( low2018.high / 1128.4 ) * low2018.high,
percentage: (0.82467216 + 0.8405847 ) / 2.0,
};
println!("The all-time high of ${:.0} for Bitcoin was followed by the drop of {:.1}%.", low2015.high, low2015.percentage * 100.0 );
println!("The last all-time high of {:.0} for Bitcoin was followed by the drop of {:.1}%.", low2018.high, low2018.percentage * 100.0 );
println!("The high of 2017 was {:.2} times higher than the high of 2014. The average of the last two corrections is {:.1}%.\n", low2022.high / low2018.high, low2022.percentage * 100.0 );
println!("Should the high of 2021 be {:.2} times higher than the high of 2017 it would be at the price of ${:.0}.", low2022.high / low2018.high, low2022.high );
println!("Should the correction in the 2022-2023 be that of {:.2}% from the top of {:.0}, the bottom would be at ${:.0}.\n", low2022.percentage * 100.0, low2022.high, low2022.low );
}
struct When<T>{
year: T,
month: T,
day: T,
}
struct Low {
high: f32,
low: f32,
percentage: f32
}
这一次我从编译器得到以下消息:
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:3:23
|
3 | let HALVING2016: When<T> = When {
| ^ not found in this scope
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:9:20
|
9 | let HIGH2017: When<T> = When {
| ^ not found in this scope
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:15:23
|
15 | let HALVING2020: When<T> = When {
| ^ not found in this scope
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:44:20
|
44 | let high2021: When<T> = When {
| ^ not found in this scope
我被困住了。为什么我必须添加? In an example from this textbook 他们的代码没有它们也能正常工作。
附:我很清楚在命名时违反约定 我的变量带有大写字母,但这超出了问题的范围。
【问题讨论】: