【发布时间】:2026-01-11 17:55:01
【问题描述】:
我有一个Vec 的结构,我想从位置 x 到 y:
fn main() {
#[derive(Debug)]
struct A {
name: String,
}
let x = A { name: "X".to_string() };
let y = A { name: "Y".to_string() };
let z = A { name: "Z".to_string() };
let v = vec!(x,y,z);
println!("{:?}", v[0..1]);
}
这是Rust Playground link for it。
我收到了错误:
error[E0277]: the size for values of type `[A]` cannot be known at compilation time
--> src/main.rs:13:5
|
13 | println!("{:?}", v[0..1]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
如何确保在编译时知道大小?我找不到任何明显的答案,因此我认为我缺少一些基本的东西。
【问题讨论】:
标签: vector indexing rust range slice