• 0 Posts
  • 718 Comments
Joined 1 year ago
cake
Cake day: June 11th, 2023

help-circle


  • Your return will return from the function, not from the for loop as you probably assume. The for loop itself does not return a value. Only loop based loops can use break to return values, other loops do not.

    You also forgot the let keyword in your assignment

    I assume you want to return the value of the href attribute for the first node that has one? In that case you want something like

    fn get_first_href_value(link_nodes: Select) -> Option<String> {
            for node in link_nodes {
                if let Some(href_value) = node.value().attr("href") {
                    return Some(href_value.into());
                }
            }
    
            None
    }
    

    or, more idiomatically

    fn get_first_href_value(link_nodes: Select) -> Option<String> {
        link_nodes.into_iter().find_map(|node| node.value().attr("href")).map(|v| v.to_string())
    }