Reputation: 44739
I've written this:
/* Turn a Vec<Vec<u8>> into a Vec<String>, without re-allocating the inner bytes. */
let lines: Vec<String> = u8_lines.into_iter()
.filter_map(|u8_line| match String::from_utf8(u8_line) {
Ok(u8_line) => Some(u8_line),
Err(_) => None,
})
.collect();
Is there a way of simplifying this by replacing the match
or the filter_map
?
Upvotes: 0
Views: 116
Reputation: 40804
Use can use Result::ok
to turn a Result<T, E>
into an Option<T>
:
/* Turn a Vec<Vec<u8>> into a Vec<String>, without re-allocating the inner bytes. */
let lines: Vec<String> = u8_lines.into_iter()
.filter_map(|u8_line| String::from_utf8(u8_line).ok())
.collect();
Upvotes: 2