Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as a struct field

Tags:

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 

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?

like image 957
user19018 Avatar asked May 15 '15 15:05

user19018


People also ask

Can you make a struct an array?

Create an Array of struct Using the malloc() Function in C There is another way to make an array of struct in C. The memory can be allocated using the malloc() function for an array of struct . This is called dynamic memory allocation.

Can we create array of structure in C?

Overview. A structure in C is a valuable user-defined data type used for storing information. It can be coupled with an array to create an array of structures.


1 Answers

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

Rust has a couple different array-ish types.

  • 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.
like image 172
Manishearth Avatar answered Sep 20 '22 15:09

Manishearth