数组作为结构域 [英] Array as a struct field

查看:46
本文介绍了数组作为结构域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Rust 中创建一个非二叉树结构.这是一个尝试

I would like to create a non binary tree structure in Rust. Here is a try

struct TreeNode<T> {
    tag : T,
    father : Weak<TreeNode<T>>,
    childrenlists : [Rc<TreeNode<T>>]
}

不幸的是,这不能编译.

Unfortunately, this does not compile.

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.

The code compiles if we replace an array with a Vec. However, the structure is immutable and I do not need an overallocated Vec.

我听说在编译时可能有一个大小未知的结构字段,只要它是唯一的.我们该怎么做?

I heard it could be possible to have a struct field with size unknown at compile time, provided it is unique. How can we do it?

推荐答案

Rust 没有可变长度(堆栈)数组的概念,您似乎在这里尝试使用它.

Rust doesn't have the concept of a variable-length (stack) array, which you seem to be trying to use here.

Rust 有几种不同的数组类型.

Rust has a couple different array-ish types.

  • Vec(向量"):动态调整大小;在堆上动态分配.这可能就是您想要使用的.使用 Vec::with_capacity(foo) 对其进行初始化以避免过度分配(这会创建一个具有给定容量的空向量).
  • [T;n] ("array"):静态大小;住在堆栈上.您需要在编译时知道大小,所以这对您不起作用(除非我错误地分析了您的情况).
  • [T](切片"):未调整大小;通常从 &[T] 使用.这是对内存中某处连续 T 集的视图.您可以通过引用数组或向量(称为获取数组/向量的切片"),甚至查看数组/向量的子集来获取它.[T] 没有大小,不能直接用作变量(它可以用作无大小结构的成员),但您可以从指针后面查看它.引用 [T] 的指针是 fat ;即他们有一个额外的长度字段.&[T] 如果要存储对现有数组的引用会很有用;但我不认为这就是你想要在这里做的事情.
  • Vec<T> ("vector"): Dynamically sized; dynamically allocated on the heap. This is probably what you want to use. Initialize it with Vec::with_capacity(foo) to avoid overallocation (this creates an empty vector with the given capacity).
  • [T; n] ("array"): Statically sized; lives on the stack. You need to know the size at compile time, so this won't work for you (unless I've misanalysed your situation).
  • [T] ("slice"): Unsized; usually used from &[T]. This is a view into a contiguous set of Ts in memory somewhere. You can get it by taking a reference to an array, or a vector (called "taking a slice of an array/vector"), or even taking a view into a subset of the array/vector. Being unsized, [T] can't be used directly as a variable (it can be used as a member of an unsized struct), but you can view it from behind a pointer. Pointers referring to [T] are fat ; i.e. they have an extra field for the length. &[T] would be useful if you want to store a reference to an existing array; but I don't think that's what you want to do here.

这篇关于数组作为结构域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆