我希望将两个f64
数字加在一起,但以下代码会产生错误。我怎样才能将这两个花车一起添加?
代码:
total_duration = 0.925338 + 0.741495;
错误:
src/main.rs:56:26: 56:57 error: the trait `core::ops::Add<_>` is not implemented for the type `f64` [E0277]
src/main.rs:56 total_duration = r.elapsed_time + total_duration;
答案 0 :(得分:4)
可能是因为您将f64
与f32
混合在一起。
您可能必须在执行此类添加时指定类型:
let total_duration = 0.925338f64 + 0.741495f64;
添加两个已经是f64类型的变量时,您不必这样做:
let x: f64 = 0.925338;
let y: f64 = 0.741495;
let total_duration = x + y;
我在使用你的代码时没有收到错误,你的编译器是最新的吗?