What does String.iter() do in Rust?

2019-08-24 03:28发布

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.

标签: rust
1条回答
小情绪 Triste *
2楼-- · 2019-08-24 04:05

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.

查看更多
登录 后发表回答