Adham
Adham

Reputation: 64904

What does String.iter() do in Rust?

I am getting confused with some code from Rust but I am managing it. But I have problem in understanding how the function iter() work in Rust. What is the possible result for a String?

Edit

impl Selector {
    pub fn specificity(&self) -> Specificity {
        // http://www.w3.org/TR/selectors/#specificity
        let Selector::Simple(ref simple) = *self;
        let a = simple.id.iter().len();
        let b = simple.class.len();
        let c = simple.tag_name.iter().len();
        (a, b, c)
    }
}

Where simple.id is string.

Upvotes: 0

Views: 1532

Answers (1)

Shepmaster
Shepmaster

Reputation: 431669

You seem to be referencing code from Robinson, specifically this file.

Note that SimpleSelector is defined as:

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

So id is not a String, but an Option<String>.

iter on Option is defined as:

Returns an iterator over the possibly contained value.

This allows you to write code that will happen if the value is Some and will not happen if it is None.

Upvotes: 2

Related Questions