Rustlings traits4.rs为什么impl Trait语法有效,但Trait绑定语法或where子句无效



我正在进行Rustlings课程培训4.rs练习。任务基本上是为compare_license_types函数选择正确的签名。使用以下impl Trait语法效果良好:

pub trait Licensed {
fn licensing_info(&self) -> String {
"some information".to_string()
}
}
struct SomeSoftware {}
struct OtherSoftware {}
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool
{
software.licensing_info() == software_two.licensing_info()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compare_license_information() {
let some_software = SomeSoftware {};
let other_software = OtherSoftware {};
assert!(compare_license_types(some_software, other_software));
}
#[test]
fn compare_license_information_backwards() {
let some_software = SomeSoftware {};
let other_software = OtherSoftware {};
assert!(compare_license_types(other_software, some_software));
}
}

如果我将函数签名更改为使用特征绑定语法或where子句,它将不再编译:

fn compare_license_types<T: Licensed>(software: T, software_two: T) -> bool {}
// Or
fn compare_license_types<T>(software: T, software_two: T) -> bool
where T: Licensed {}

两者都因编译错误而失败:

error[E0308]: mismatched types
--> exercises/traits/traits4.rs:37:54
|
37 |         assert!(compare_license_types(some_software, other_software));
|                                                      ^^^^^^^^^^^^^^ expected struct `SomeSoftware`, found struct `OtherSoftware`
error[E0308]: mismatched types
--> exercises/traits/traits4.rs:45:55
|
45 |         assert!(compare_license_types(other_software, some_software));
|                                                       ^^^^^^^^^^^^^ expected struct `OtherSoftware`, found struct `SomeSoftware`

我在这里错过了什么?

fn compare_license_types<T: Licensed>(software: T, software_two: T) -> bool { ...

告诉编译器softwaresoftware_two都具有类型T,其中T可以是实现Licensed的任何类型。当传入类型为SomeSoftwaresome_software时,Rust推断T的类型必须为SomeSoftware。但是参数software_two也具有类型T,即类型SomeSoftware。但您传递给它的是一个类型为OtherSoftware的参数。这就是它不编译的原因。

您可以通过将compare_license_types通用于两种不同的类型来解决此问题,这两种类型都实现了Licensed:

fn compare_license_types<T: Licensed, U: Licensed>(software: T, software_two: U) -> bool { ...

这就是CCD_ 19语法隐含地所做的。

最新更新