gigapixel

joined 2 years ago
[–] gigapixel@mastodontti.fi 0 points 8 months ago* (last edited 8 months ago) (2 children)

@dontblink If you really want to do it in a loop, you could do something like

fn get\_links(link\_nodes: Select) -\> Option\<String\> {  
 let mut rel\_permalink: Option\<String\> = None;  
 for node in link\_nodes {  
 if let Some(link) = node.value().attr("href")? {  
 rel\_permalink = Some(String::from(link));  
 break  
 }  
 };  
 rel\_permalink  
}  

That said, your function name suggest you want _all_ links, so some kind of collection of links. Is this the case?

[–] gigapixel@mastodontti.fi 0 points 8 months ago (3 children)

@dontblink Looks like there's some weird stuff between my instance and yours in the formatting. There shouldn't be any backslashes anywhere

[–] gigapixel@mastodontti.fi 0 points 8 months ago (4 children)

@dontblink Here's a little explanation for the methods
- into\_iter : converts Select into an iterator (a for loop does this implicitly)
- filter\_map: takes an iterator and constructs an iterator from an Fn(T) -\> Option\<S\> where the emitted elements are the elements for which applying the function/closure is Some
- next: takes the next element of the iterator as an Option.

[–] gigapixel@mastodontti.fi 0 points 8 months ago (5 children)

@dontblink It's a little unclear what you want to do. It looks like Select implements into iterator. As far as I can parse your code you want to get the first node with a href element. In that case you should do:

link\_nodes.into\_iter().filter\_map(|node| node.value().attr("href")).map(String::from).next()