我有一个非常大的结构(在堆上和堆栈上),我在函数中使用。大多数时候,我想要该结构的一个简单变量,因为我直接对大结构进行操作。然而,在某一时刻,我被迫(通过函数签名)将此结构传递到切片内部。
struct VeryBig(Vec<String>, [u64; 50]);
fn takes_slice(_: &[VeryBig]) {}
fn main() {
let foo = VeryBig(vec!["Ferris".to_string(); 100], [27; 50]);
// Use `foo` directly a bunch of times
takes_slice(&foo); // <-- mismatched type
// Use `foo` directly a bunch of times
}
Run Code Online (Sandbox Code Playgroud)
这显然并且可以理解地导致了这个错误:
error[E0308]: mismatched types
--> src/main.rs:10:17
|
10 | takes_slice(&foo); // <-- mismatched type
| ^^^^ expected slice, found struct `VeryBig`
|
= note: expected type `&[VeryBig]`
found type `&VeryBig`
Run Code Online (Sandbox Code Playgroud)
所以我想知道:解决这个问题的最佳方法是什么?我可以只制作foo
一个[VeryBig; 1]
,但这意味着我必须在foo[0] …