K
K
keddad2019-06-06 22:21:21
Rust
keddad, 2019-06-06 22:21:21

What is the purpose of Empty and Elem in a primitive data structure?

I have a simple list:

pub enum List {
    Empty,
    Elem(i32, Box<List>),
}

Why should I write Empty here if it compiles without it?
What is elem? Is it some kind of data structure? I did not declare it additionally and did not find it mentioned in the documentation, but it works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Lesnikov, 2019-06-07
@keddad

> Why should I write Empty here if it compiles without it?
The enumeration itself without Elem compiles, but constructing a real list without a terminating empty node will not work, because it would have to be infinite.
> What is Elem?
This is one of the variants of the declared List enumeration, which contains two unnamed fields of clear types.
> I did not declare it additionally and did not find it mentioned in the documentation, but it works.
This is just the simplest enum syntax, which is described in any Rust textbook, for example, in the Rust Book: https://doc.rust-lang.org/book/ch06-01-defining-an...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question