What do the keywords "Some" and "Simple" do in Rust?

I'm trying to get more used to programming by asking myself a directed challenge and the one I found at http://limpet.net/mbrubeck/2014/08/08/toy-layout-engine-1.html seemed to be a good match. Since I am focused on learning Python, I figured it would be good practice to convert the tutorial to Python. At the same time, I thought it would teach me something about Rust and about reading code in general. Victory is all around!

However, there are some things that I find it difficult to understand what exactly they do. In particular, they are keywords (are they even keywords?) "Some" and "Simple" appear in the presented code, but I have no idea what they do. For example:

enum Selector {
    Simple(SimpleSelector),
}

struct SimpleSelector {
    tag_name: Option<String>,
    id: Option<String>,
    class: Vec<String>,
}

      

I understand that an enum is a way of storing data that can (exactly) be one of several possible types, but I don't understand what it means here.

Another thing that appears in the author's code is (for example)

match self.next_char() {
    '#' => {
        self.consume_char();
        selector.id = Some(self.parse_identifier());
    }

      

where in this case I have no idea what the term "Some" does. I tried to browse the official Rust documentation found at http://www.rust-lang.org/ --- however by no means can I find a description of these terms in this documentation, although "Some" is used in the documentation!

So what do these terms do? More generally, where exactly is the list of Rust keywords since the search term "words for the rust programming language" doesn't help?

+3


source to share


2 answers


Rust features Algebraic data types , in short, data types with several possible forms, for example:

 enum OptionInt {
     None,
     Some(int)
 }

      

Is a data type that is either None

(single point value) or Some(int)

(a int

). In this case, None

they Some

are data constructors: when applied to a value (or without any value in the case None

), they generate a value of type OptionInt

.

These data constructors will also render according to the pattern:



match an_option_int {
    Some(an_integer) => 3 * an_integer,
    None => 0
}

      

is an expression that will create int

either:

  • 0

    if an_option_int

    containsNone

  • or 6

    if an_option_int

    containsSome(2i)

These features are also known as flagged unions.

+8


source


These are not keywords; they give names to the listing options. Relevant section in the manual . The list of keywords is in the link .



+1


source







All Articles