简体   繁体   English

f32没有实现减法?

[英]Subtraction not implemented for f32?

When compiling the following code: 编译以下代码时:

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();
}

I get an error saying: 我收到一个错误说:

the trait core::ops::Sub<_> is not implemented for the type f32 对于类型f32没有实现trait core::ops::Sub<_>

Why is this? 为什么是这样?

Rust is stricter than some other languages when it comes to manipulating primitive types. 在处理原始类型时,Rust比其他语言更严格。 Most math operators require the same type on both sides (with the exception of bit shifts, which expect a usize as the right-hand side operand). 大多数数学运算符在两侧都需要相同的类型(除了位移,它们期望将usize作为右侧操作数)。 Rust will not automatically cast values from one primitive numeric type to another: you must insert an explicit cast in the code. Rust不会自动将值从一种原始数字类型转换为另一种:您必须在代码中插入显式强制转换。 This code demonstrates the situation: 此代码演示了这种情况:

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

It fails to compile with the following errors: 它无法编译时出现以下错误:

<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

Your situation is similar, but it has the particularity that you're mixing integers and floats. 您的情况类似,但它具有混合整数和浮点数的特殊性。 In Rust, integer and float literals are assigned a type based on context. 在Rust中,为整数和浮点文字分配基于上下文的类型。 That's why I could set a to 2 and b to 3 above: 2 is not always an i32 , but it is implicitly typed as i32 if the context requires it. 这就是为什么我可以将a设置为2b32并不总是i32 ,但如果上下文需要它,则隐式输入为i32

In your case, you're trying to subtract an integer from an f32 . 在你的情况下,你试图从f32减去一个整数。 The error message mentions Sub<_> ; 错误消息提到Sub<_> ; that _ represents the type of the 4 literal that the compiler wasn't able to figure out. _表示编译器无法弄清楚的4字面的类型。

The solution is simply to use float literals instead of integer literals: 解决方案只是使用浮点文字而不是整数文字:

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<_>>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM