f32没有实现减法?

loc*_*kie 5 rust

编译以下代码时:

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

为什么是这样?

Fra*_*gné 10

在处理原始类型时,Rust比其他语言更严格.大多数数学运算符在两侧都需要相同的类型(除了位移,它们期望usize作为右侧操作数).Rust不会自动将值从一种原始数字类型转换为另一种:您必须在代码中插入显式强制转换.此代码演示了这种情况:

fn main(){
    let a: i32 = 2;
    let b: i8 = 3;
    println!("{}", a + b);
}
Run Code Online (Sandbox Code Playgroud)

它无法编译时出现以下错误:

<anon>:4:24: 4:25 error: mismatched types:
 expected `i32`,
    found `i8`
(expected i32,
    found i8) [E0308]
<anon>:4     println!("{}", a + b);
                                ^
<std macros>:2:25: 2:56 note: in this expansion of format_args!
<std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
<anon>:4:5: 4:27 note: in this expansion of println! (defined in <std macros>)
<anon>:4:24: 4:25 help: see the detailed explanation for E0308
<anon>:4:20: 4:25 error: the trait `core::ops::Add<i8>` is not implemented for the type `i32` [E0277]
<anon>:4     println!("{}", a + b);
                            ^~~~~
<std macros>:2:25: 2:56 note: in this expansion of format_args!
<std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
<anon>:4:5: 4:27 note: in this expansion of println! (defined in <std macros>)
<anon>:4:20: 4:25 help: see the detailed explanation for E0277
Run Code Online (Sandbox Code Playgroud)

您的情况类似,但它具有混合整数和浮点数的特殊性.在Rust中,为整数和浮点文字分配基于上下文的类型.这就是为什么我可以设置a2b3上面:2并不总是一件i32,但因为它是隐式类型i32如果上下文需要它.

在你的情况下,你试图从一个减去一个整数f32.错误消息提到Sub<_>; 该_代表的类型4文字,编译器无法弄清楚.

解决方案只是使用浮点文字而不是整数文字:

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.0) / 2.0)
        .map(|s| s as i32)
        .collect::<Vec<_>>();
}
Run Code Online (Sandbox Code Playgroud)