小编Léo*_*lli的帖子

找到用于货物测试的资源

我正在与文件交互的项目,我想使用文本文件来测试我的工作.但是,测试不会从tests/目录运行,因此在运行时我无法可靠地找到它们cargo run.
是否tests/通过始终从根目录下运行测试处理这个(这似乎是如此,但我没有发现任何东西,证明它)?

testing rust rust-cargo

12
推荐指数
1
解决办法
2180
查看次数

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

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

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));
}
Run Code Online (Sandbox Code Playgroud)

这是编译器输出

error[E0308]: mismatched types
 --> src/main.rs:5:13
  |
5 |     if x == 0 {
  |             ^ expected type parameter, …
Run Code Online (Sandbox Code Playgroud)

generics int traits rust

7
推荐指数
2
解决办法
1905
查看次数

标签 统计

rust ×2

generics ×1

int ×1

rust-cargo ×1

testing ×1

traits ×1