算术运算的Rust泛型[重复]

问题描述 投票:1回答:1

我写了一个通用函数来检查给定的数字是否是偶数:

use std::ops::Rem;

fn main() {
    let x: u16 = 31;
    let y: u32 = 40;

    println!("x = {}, y = {}", is_even(x), is_even(y));
}

fn is_even<T: Rem<Output = T> + PartialEq>(n: T) -> bool {
    n % 2 == 0
}

它产生编译器错误:

error[E0308]: mismatched types
  --> src/main.rs:11:9
   |
11 |     n % 2 == 0
   |         ^ expected type parameter, found integral variable
   |
   = note: expected type `T`
              found type `{integer}`

error[E0308]: mismatched types
  --> src/main.rs:11:14
   |
11 |     n % 2 == 0
   |              ^ expected type parameter, found integral variable
   |
   = note: expected type `T`
              found type `{integer}`

由于这是使用T与具体的i32值(2和0)的错误,我写了另一个版本的is_even像这样:

fn is_even<T: Rem<Output = T> + PartialEq> (n: T, with: T, output: T) -> bool {
    n % with == output
}

这产生了所需的输出,但现在使用is_even是错综复杂的:is_even(x, 2, 0)。如何使我的原始版本的is_even工作?

我确实认识到模运算符足够通用,可以直接使用u16u32值;不需要像is_even这样的函数 - 但是我写它是为了理解泛型如何工作。

generics rust
1个回答
3
投票

你对T的假设比你在类型中表达的要多得多。你已经正确地将T约束到RemPartialEq,所以你可以使用%==算子,但你也假设有特殊值02属于那种类型。

2的存在没有特征,但你可以使用num-traits crate来找到具有01的类型的特征。假设泛型类型也有加法,你可以通过向自身添加2来确保它具有one

extern crate num_traits;

use num_traits::{Zero, One};
use std::ops::{Rem, Add};

fn main() {
    let x: u16 = 31;
    let y: u32 = 40;

    println!("x = {}, y = {}", is_even(x), is_even(y));
}

fn is_even<T> (n: T) -> bool 
where
    T: Rem<Output = T> + PartialEq + One + Zero + Add + Copy
{
    let one: T = One::one();
    n % (one + one) == Zero::zero()
}

注意,我还制作了T: Copy,因此它不需要在加法表达式中引用。对于大多数数字类型,这是一个合理的假设。


在不使用第三方包的情况下,您还可以使用其他类型的02的值,例如u8,并确保您的泛型类型可以从中转换为。

fn is_even<T> (n: T) -> bool 
where
    T: Rem<Output = T> + PartialEq + From<u8>
{
    let two: T = 2u8.into();
    let zero: T = 0u8.into();
    n % two == zero
}

我更喜欢这个版本,因为约束T: From<u8>并没有真正传达有关函数正在做什么的任何有用信息。它将类型与深奥的实现细节联系起来,而第一个版本的约束则精确地描述了需要支持的算术运算。

© www.soinside.com 2019 - 2024. All rights reserved.