我希望能在Rust中创建一个非二叉树结构。以下是我的尝试:
struct TreeNode<T> {
tag : T,
father : Weak<TreeNode<T>>,
childrenlists : [Rc<TreeNode<T>>]
}
很遗憾,这段代码无法编译。
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
如果我们用
Vec
替换数组,代码就可以编译。但是,这个结构是不可变的,我不需要一个过度分配的Vec
。我听说如果它是唯一的,那么在编译时可以有一个大小未知的结构字段。我们如何做到这一点?
&[T]
通常被称为切片,我不知道如何发音[T]
。 - Shepmaster