使用泛型类型时如何使用整数文字? [英] How do I use integer number literals when using generic types?

查看:19
本文介绍了使用泛型类型时如何使用整数文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to implement a function computing the number of digits within any generic type of integer. Here is the code I came up with:

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 不能解决这个问题.

I understand that the problem comes from my use of constants within the function, but I don't understand why the trait specification as Integer doesn't solve this.

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

The documentation for Integer says it implements the PartialOrd, etc. traits with Self (which I assume refers to Integer). By using integer constants which also implement the Integer trait, aren't the operations defined, and shouldn't the compiler compile without errors?

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

I tried suffixing my constants with i32, but the error message is the same, replacing _ with i32.

推荐答案

这里有很多问题:

  1. 正如 Shepmaster 所说01 不能转换为实现 Integer 的所有内容.使用 Zero::zeroOne::one 代替.
  2. 10 绝对不能转换为任何实现 Integer 的东西,你需要使用 NumCast 来实现
  3. a/= b 不是 a = a/b 的糖,而是 Integer 不需要的单独特征.
  4. -x 是一个一元运算,它不是 Integer 的一部分,但需要 Neg 特性(因为它只对有符号类型有意义).
  1. As Shepmaster says, 0 and 1 cannot be converted to everything implementing Integer. Use Zero::zero and One::one instead.
  2. 10 can definitely not be converted to anything implementing Integer, you need to use NumCast for that
  3. a /= b is not sugar for a = a / b but an separate trait that Integer does not require.
  4. -x is an unary operation which is not part of Integer but requires the Neg trait (since it only makes sense for signed types).

这是一个实现.请注意,您需要对 Neg 进行绑定,以确保其结果与 T

Here's an implementation. Note that you need a bound on Neg, to make sure that it results in the same type as 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));
}

这篇关于使用泛型类型时如何使用整数文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆