Vorrei creare una struttura ad albero non binaria in Rust. Ecco una provaMatrice come campo struct
struct TreeNode<T> {
tag : T,
father : Weak<TreeNode<T>>,
childrenlists : [Rc<TreeNode<T>>]
}
Sfortunatamente, questo non viene compilato.
main.rs:4:1: 8:2 error: the trait `core::marker::Sized` is not implemented for the type `[alloc::rc::Rc<TreeNode<T>>]` [E0277]
main.rs:4 struct TreeNode<T> {
main.rs:5 tag : T,
main.rs:6 father : Weak<TreeNode<T>>,
main.rs:7 childrenlist : [Rc<TreeNode<T>>]
main.rs:8 }
main.rs:4:1: 8:2 note: `[alloc::rc::Rc<TreeNode<T>>]` does not have a constant size known at compile-time
main.rs:4 struct TreeNode<T> {
main.rs:5 tag : T,
main.rs:6 father : Weak<TreeNode<T>>,
main.rs:7 childrenlist : [Rc<TreeNode<T>>]
main.rs:8 }
error: aborting due to previous error
Il codice viene compilato se si sostituisce un array con un Vec
. Tuttavia, la struttura è immutabile e non ho bisogno di un numero Vec
.
Ho sentito che potrebbe essere possibile avere un campo struct con dimensioni sconosciute al momento della compilazione, a condizione che sia univoco. Come possiamo farlo?
Ho pensato che il requisito fosse "l'ultimo", ma comunque è anche il caso qui. Ho trovato https://www.reddit.com/r/rust/comments/357ji5/using_structs_with_a_dst_array_member/ –
In Rust, * le matrici * hanno una dimensione fissa, nota al momento della compilazione. Quindi non vuoi un "array". '& [T]' viene solitamente chiamato a * slice *, non so come pronunciare '[T]'. – Shepmaster
@Shepmaster Immagino che sarebbe "array non standardizzato". –