我是否必须在有符号整数的符号上进行分支,如下例所示?(在真实的程序中y
是计算改变索引指向为64K数组,而wraparound是期望的行为)
fn main() {
let mut x: u16 = 100;
let y: i8 = -1;
//x += y;
if y < 0 {
x -= i8::abs(y) as u16;
} else {
x += i8::abs(y) as u16;
}
println!("{}", x);
}
Run Code Online (Sandbox Code Playgroud) 编译以下代码时:
use std::io::*;
fn main(){
let reader = stdin();
let nums = reader.lock()
.lines().next().unwrap().unwrap()
.split_whitespace()
.map(|s| s.parse::<i32>().unwrap())
.map(|s| s as f32)
.map(|s| (s - 4) / 2)
.map(|s| s as i32)
.collect();
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说:
core::ops::Sub<_>
该类型没有实现特征f32
为什么是这样?
我有以下代码:
struct Stuff {
thing: i8
}
fn main(){
let theStuff = Stuff { thing: 1 };
println!("{}", theStuff.thing * 1.5);
}
Run Code Online (Sandbox Code Playgroud)
我在编译时得到以下内容:
error[E0277]: the trait bound `i8: std::ops::Mul<{float}>` is not satisfied
--> IntFloatMultiply.rs:7:32
|
7 | println!("{}", theStuff.thing * 1.5);
| ^ no implementation for `i8 * {float}`
|
= help: the trait `std::ops::Mul<{float}>` is not implemented for `i8`
Run Code Online (Sandbox Code Playgroud)
我已经阅读了其他一些帖子,其中包括一些非常好的内容(包括/sf/answers/3118672511/).如果我没有具体的答案,我不关心技术细节,什么或为什么.如何编译此代码以显示浮点结果?
rust ×3