我正在与文件交互的项目,我想使用文本文件来测试我的工作.但是,测试不会从tests/目录运行,因此在运行时我无法可靠地找到它们cargo run.
是否tests/通过始终从根目录下运行测试处理这个(这似乎是如此,但我没有发现任何东西,证明它)?
我想实现一个函数来计算任何泛型类型的整数中的位数.这是我提出的代码:
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)