use std::num::Float;
fn main() {
for i in 1..101 {
euler(i)
}
}
fn euler(x: i32){
let n: i32 = x;
let e: f64 = (1.0+(1.0/n)).powi(n);
println!("Euler's number where n = {} is {}", n, e);
}
我有这个代码,我无法编译。我是Rust的新手,所以请各位帮忙谢谢!
答案 0 :(得分:1)
让我们看一下错误信息:
<anon>:11:28: 11:29 error: mismatched types:
expected `_`,
found `i32`
(expected floating-point variable,
found i32) [E0308]
<anon>:11 let e: f64 = (1.0+(1.0/n)).powi(n);
^
在这里,Rust有非常好的消息:你需要提供一个浮点变量,而不是一个整数:
let e: f64 = (1.0+(1.0/n as f64)).powi(n);
// HERE ^~~~~~~~