[英]Subtraction not implemented for f32?
编译以下代码时:
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();
}
我收到一个错误说:
对于类型
f32
没有实现traitcore::ops::Sub<_>
为什么是这样?
在处理原始类型时,Rust比其他语言更严格。 大多数数学运算符在两侧都需要相同的类型(除了位移,它们期望将usize
作为右侧操作数)。 Rust不会自动将值从一种原始数字类型转换为另一种:您必须在代码中插入显式强制转换。 此代码演示了这种情况:
fn main(){
let a: i32 = 2;
let b: i8 = 3;
println!("{}", a + b);
}
它无法编译时出现以下错误:
<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
您的情况类似,但它具有混合整数和浮点数的特殊性。 在Rust中,为整数和浮点文字分配基于上下文的类型。 这就是为什么我可以将a
设置为2
和b
为3
: 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<_>>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.