Trait implementation with where clauses: 实现一个简单的 where 子句
Trait implementation with where clauses: Implementing a simple where clause
我一直在尝试通过 rust-koans 来学习 Rust,但是 运行 遇到了具有以下特征 koan 的墙:
// There is an alternate syntax for placing trait bounds on a function, the
// where clause. Let's revisit a previous example, this time using 'where'.
#[test]
fn where_clause() {
let num_one: u16 = 3;
let num_two: u16 = 4;
trait IsEvenOrOdd {
fn is_even(&self) -> bool;
}
impl IsEvenOrOdd for u16 {
fn is_even(&self) -> bool {
self % 2 == 0
}
}
fn asserts<T>(x: T, y: T) {
assert!(!x.is_even());
assert!(y.is_even());
}
asserts(num_one, num_two);
}
似乎目标是通过创建 IsEvenOrOdd
实现的通用版本来完成此代码。在这种情况下,泛型类型应该有两个边界,余数运算符和 PartialEq
运算符。因为余数右边和等价右边都是整数,所以我最终写了下面的意大利面条代码:
use std::ops::Rem;
impl<T> IsEvenOrOdd for T
where
T: Rem<u16> + Rem,
<T as Rem<u16>>::Output: PartialEq<u16>,
{
fn is_even(&self) -> bool {
self % 2 == 0
}
}
仍然 - 代码无法编译。似乎由于 T
被取消引用,我需要为取消引用的值添加界限,但我找不到任何示例说明如何做到这一点。
error[E0369]: binary operation `%` cannot be applied to type `&T`
--> src\koans/traits.rs:142:13
|
142 | self % 2 == 0
| ^^^^^^^^
|
= note: an implementation of `std::ops::Rem` might be missing for `&T`
简而言之:解决这个 koan 的惯用 Rust 方法是什么?
我认为您可能误解了本练习的意图。你要的是这个:
fn main() {
let num_one: u16 = 3;
let num_two: u16 = 4;
trait IsEvenOrOdd {
fn is_even(&self) -> bool;
}
impl IsEvenOrOdd for u16 {
fn is_even(&self) -> bool {
self % 2 == 0
}
}
fn asserts<T>(x: T, y: T)
where T: IsEvenOrOdd {
assert!(!x.is_even());
assert!(y.is_even());
}
asserts(num_one, num_two);
}
我是怎么得出这个结论的?尝试编译和 运行 您发布的原始代码会导致以下错误:
error[E0599]: no method named `is_even` found for type `T` in the current scope
--> src/main.rs:16:20
|
16 | assert!(!x.is_even());
| ^^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `is_even`, perhaps you need to implement it:
candidate #1: `main::IsEvenOrOdd`
这个错误告诉我们要调用is_even
方法,你必须实现IsEvenOrOdd
。您发布的示例顶部的评论说在 函数 上使用 where
子句。将 where
子句添加到函数 asserts
可以解决您的问题并完成练习。
我一直在尝试通过 rust-koans 来学习 Rust,但是 运行 遇到了具有以下特征 koan 的墙:
// There is an alternate syntax for placing trait bounds on a function, the
// where clause. Let's revisit a previous example, this time using 'where'.
#[test]
fn where_clause() {
let num_one: u16 = 3;
let num_two: u16 = 4;
trait IsEvenOrOdd {
fn is_even(&self) -> bool;
}
impl IsEvenOrOdd for u16 {
fn is_even(&self) -> bool {
self % 2 == 0
}
}
fn asserts<T>(x: T, y: T) {
assert!(!x.is_even());
assert!(y.is_even());
}
asserts(num_one, num_two);
}
似乎目标是通过创建 IsEvenOrOdd
实现的通用版本来完成此代码。在这种情况下,泛型类型应该有两个边界,余数运算符和 PartialEq
运算符。因为余数右边和等价右边都是整数,所以我最终写了下面的意大利面条代码:
use std::ops::Rem;
impl<T> IsEvenOrOdd for T
where
T: Rem<u16> + Rem,
<T as Rem<u16>>::Output: PartialEq<u16>,
{
fn is_even(&self) -> bool {
self % 2 == 0
}
}
仍然 - 代码无法编译。似乎由于 T
被取消引用,我需要为取消引用的值添加界限,但我找不到任何示例说明如何做到这一点。
error[E0369]: binary operation `%` cannot be applied to type `&T`
--> src\koans/traits.rs:142:13
|
142 | self % 2 == 0
| ^^^^^^^^
|
= note: an implementation of `std::ops::Rem` might be missing for `&T`
简而言之:解决这个 koan 的惯用 Rust 方法是什么?
我认为您可能误解了本练习的意图。你要的是这个:
fn main() {
let num_one: u16 = 3;
let num_two: u16 = 4;
trait IsEvenOrOdd {
fn is_even(&self) -> bool;
}
impl IsEvenOrOdd for u16 {
fn is_even(&self) -> bool {
self % 2 == 0
}
}
fn asserts<T>(x: T, y: T)
where T: IsEvenOrOdd {
assert!(!x.is_even());
assert!(y.is_even());
}
asserts(num_one, num_two);
}
我是怎么得出这个结论的?尝试编译和 运行 您发布的原始代码会导致以下错误:
error[E0599]: no method named `is_even` found for type `T` in the current scope
--> src/main.rs:16:20
|
16 | assert!(!x.is_even());
| ^^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `is_even`, perhaps you need to implement it:
candidate #1: `main::IsEvenOrOdd`
这个错误告诉我们要调用is_even
方法,你必须实现IsEvenOrOdd
。您发布的示例顶部的评论说在 函数 上使用 where
子句。将 where
子句添加到函数 asserts
可以解决您的问题并完成练习。