【发布时间】:2018-06-20 22:10:50
【问题描述】:
我无法确定数组的正确类型(不是Vec)。以下代码无法编译:
fn sum(a: [f32]) -> f32 {
return 3.0;
}
fn main() {
let x = [0.0, 1.0, 2.0];
print!("{}\n", sum(x));
}
error[E0277]: the trait bound `[f32]: std::marker::Sized` is not satisfied
--> src/main.rs:1:8
|
1 | fn sum(a: [f32]) -> f32 {
| ^ `[f32]` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[f32]`
= note: all local variables must have a statically known size
error[E0308]: mismatched types
--> src/main.rs:7:24
|
7 | print!("{}\n", sum(x));
| ^ expected slice, found array of 3 elements
|
= note: expected type `[f32]`
found type `[{float}; 3]`
sum 中的a 的合适类型是什么?
【问题讨论】:
标签: rust