gpt4 book ai didi

rust - 为什么我会收到错误 E0277 : the size for values of type `[{integer}]` cannot be known at compilation time?

转载 作者:行者123 更新时间:2023-11-29 08:35:53 28 4
gpt4 key购买 nike

在下面的代码中我得到了错误:

error[E0277]: the size for values of type `[{integer}]` cannot be
known at compilation time at the line `for n in numbers[1..] {`

我四处寻找,但一无所获。

fn main() {
let mut numbers = Vec::new();
numbers.push(1);
numbers.push(32);
numbers.push(43);
numbers.push(42);
// ... And many more
println!("{:?}", numbers); // Sanity

let mut sum = 0;

// Problem code area
for n in numbers[1..] {
sum = sum + n;
}
// Problem code area

println!("{}", sum);
}

此外,如果我将问题行替换为以下内容(添加 &* 用于所有权/借用和取消引用)

for n in &numbers[1..] {
sum = sum + *n;
}

为什么前一种方式编译失败?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com