【发布时间】:2019-08-31 16:24:28
【问题描述】:
考虑这个例子:
fn main() {
let v: Vec<i32> = vec![1, 2, 3, 4, 5];
let b: i32 = (&v[2]) * 4.0;
println!("product of third value with 4 is {}", b);
}
这失败了,因为float 不能与&i32 相乘。
error[E0277]: cannot multiply `{float}` to `&i32`
--> src\main.rs:3:23
|
3 | let b: i32 = (&v[2]) * 4.0;
| ^ no implementation for `&i32 * {float}`
|
= help: the trait `std::ops::Mul<{float}>` is not implemented for `&i32`
但是当我将浮点数更改为 int 时,它工作正常。
fn main() {
let v: Vec<i32> = vec![1, 2, 3, 4, 5];
let b: i32 = (&v[2]) * 4;
println!("product of third value with 4 is {}", b);
}
编译器是否实现了&i32和i32之间的操作?
如果是,那么在这种类型安全的语言中,这个操作如何证明是合理的?
【问题讨论】:
标签: rust