generics - 使用泛型类型时如何使用整数文字?

标签 generics int rust traits

我想实现一个函数来计算任何泛型整数中的位数。这是我想出的代码:

extern crate num;
use num::Integer;

fn int_length<T: Integer>(mut x: T) -> u8 {
    if x == 0 {
        return 1;
    }

    let mut length = 0u8;
    if x < 0 {
        length += 1;
        x = -x;
    }

    while x > 0 {
        x /= 10;
        length += 1;
    }

    length
}

fn main() {
    println!("{}", int_length(45));
    println!("{}", int_length(-45));
}

这是编译器的输出

error[E0308]: mismatched types
 --> src/main.rs:5:13
  |
5 |     if x == 0 {
  |             ^ expected type parameter, found integral variable
  |
  = note: expected type `T`
             found type `{integer}`

error[E0308]: mismatched types
  --> src/main.rs:10:12
   |
10 |     if x < 0 {
   |            ^ expected type parameter, found integral variable
   |
   = note: expected type `T`
              found type `{integer}`

error: cannot apply unary operator `-` to type `T`
  --> src/main.rs:12:13
   |
12 |         x = -x;
   |             ^^

error[E0308]: mismatched types
  --> src/main.rs:15:15
   |
15 |     while x > 0 {
   |               ^ expected type parameter, found integral variable
   |
   = note: expected type `T`
              found type `{integer}`

error[E0368]: binary assignment operation `/=` cannot be applied to type `T`
  --> src/main.rs:16:9
   |
16 |         x /= 10;
   |         ^ cannot use `/=` on type `T`

我知道问题出在我在函数中使用常量,但我不明白为什么将特征指定为 Integer 不能解决这个问题。

The documentation for Integer说它实现了 PartialOrd 等特性,带有 Self(我假设它指的是 Integer)。通过使用也实现了 Integer 特征的整数常量,是否定义了操作,编译器编译时不应该没有错误吗?

我尝试用 i32 为我的常量添加后缀,但错误消息是相同的,将 _ 替换为 i32

最佳答案

这里有很多问题:

  1. As Shepmaster says01 不能转换为所有实现 Integer 的东西。请改用 Zero::zeroOne::one
  2. 10 绝对不能转换为任何实现 Integer 的东西,你需要为此使用 NumCast
  3. a/= b 不是 a = a/b 的糖,而是 Integer 不需要的单独特征。
  4. -x 是一元运算,它不是 Integer 的一部分,但需要 Neg 特性(因为它只对有符号类型有意义).

这是一个实现。请注意,您需要对 Neg 进行绑定(bind),以确保它产生与 T

相同的类型
extern crate num;

use num::{Integer, NumCast};
use std::ops::Neg;

fn int_length<T>(mut x: T) -> u8
where
    T: Integer + Neg<Output = T> + NumCast,
{
    if x == T::zero() {
        return 1;
    }

    let mut length = 0;
    if x < T::zero() {
        length += 1;
        x = -x;
    }

    while x > T::zero() {
        x = x / NumCast::from(10).unwrap();
        length += 1;
    }

    length
}

fn main() {
    println!("{}", int_length(45));
    println!("{}", int_length(-45));
}

关于generics - 使用泛型类型时如何使用整数文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565440/

相关文章:

rust-buildgen requestAnimationFrameLoop 无法在闭包内使用结构方法

generics - 使用泛型的类型别名类型的构造函数

c# - 将选定的 T 属性从一个 IQueryable<T> 获取到一个 IQueryable<MyClass>

c# - 运算符 '&' 不能应用于类型 'T' 和 'T' 的操作数

c - 是否可以替换 double 并仅使用 C 中的整数编写程序,并具有相同的输出?

rust - 不同具体类型泛型的 Vec

java - 我应该使用问号类型参数还是抑制原始类型警告?

Python3 将 Unicode String 转换为 int 表示

c - C 中 short int 的最小值/最大值

ld - 带有 Rust 的可移植二进制文件