
Fascinated by the theory, the practice, and the craft of Software Engineering. Currently: Rust and Python at @harrison_ai Formerly: @mozilla He/Him
- Tweets1.2K
- Following314
- Followers497
- Likes1.1K
![// Iterators over Option<>s can be collected in different ways:
let a = [Some(1), Some(2), Some(3)];
let b = [Some(1), None, Some(3)];
// 1. Just collect the Option values:
let _: Vec<Option<i32>> = a.into_iter().collect(); // [Some(1), Some(2), Some(3)]
let _: Vec<Option<i32>> = b.into_iter().collect(); // [Some(1), None, Some(3)]
// 2. Collect and unwrap the Some()s, stopping at None:
let _: Option<Vec<i32>> = a.into_iter().collect(); // Some([1, 2, 3])
let _: Option<Vec<i32>> = b.into_iter().collect(); // None](https://pbs.twimg.com/media/E-qnRPhWEAAb104.jpg?name=small&format=webp)
ALT // Iterators over Option<>s can be collected in different ways: let a = [Some(1), Some(2), Some(3)]; let b = [Some(1), None, Some(3)]; // 1. Just collect the Option values: let _: Vec<Option<i32>> = a.into_iter().collect(); // [Some(1), Some(2), Some(3)] let _: Vec<Option<i32>> = b.into_iter().collect(); // [Some(1), None, Some(3)] // 2. Collect and unwrap the Some()s, stopping at None: let _: Option<Vec<i32>> = a.into_iter().collect(); // Some([1, 2, 3]) let _: Option<Vec<i32>> = b.into_iter().collect(); // None
![// Iterators over Result<>s can be collected in different ways:
let a = [Ok(1), Ok(2), Ok(3)];
let b = [Ok(1), Err("!"), Ok(3)];
// 1. Just collect the Result values:
let _: Vec<Result<i32, &str>> = a.into_iter().collect(); // [Ok(1), Ok(2), Ok(3)]
let _: Vec<Result<i32, &str>> = b.into_iter().collect(); // [Ok(1), Err("!"), Ok(3)]
// 2. Collect Ok()s, stopping at Err():
let _: Result<Vec<i32>, &str> = a.into_iter().collect(); // Ok([1, 2, 3])
let _: Result<Vec<i32>, &str> = b.into_iter().collect(); // Err("!")](https://pbs.twimg.com/media/E-qoKgrWUAEA2_0.jpg?name=small&format=webp)
ALT // Iterators over Result<>s can be collected in different ways: let a = [Ok(1), Ok(2), Ok(3)]; let b = [Ok(1), Err("!"), Ok(3)]; // 1. Just collect the Result values: let _: Vec<Result<i32, &str>> = a.into_iter().collect(); // [Ok(1), Ok(2), Ok(3)] let _: Vec<Result<i32, &str>> = b.into_iter().collect(); // [Ok(1), Err("!"), Ok(3)] // 2. Collect Ok()s, stopping at Err(): let _: Result<Vec<i32>, &str> = a.into_iter().collect(); // Ok([1, 2, 3]) let _: Result<Vec<i32>, &str> = b.into_iter().collect(); // Err("!")
![// () items can be collected into a single (), since it carries no information:
let _: () = [(), (), (), (), ()].into_iter().collect();
// This also works for Result<(), E>, collecting the Ok(()) into a single Ok(()):
let _: Result<(), &str> = [Ok(()), Ok(()), Ok(())].into_iter().collect(); // Ok(())
let _: Result<(), &str> = [Ok(()), Err("?"), Err("??")].into_iter().collect(); // Err("?")
// And for Option<()> too:
let _: Option<()> = [Some(()), Some(()), Some(())].into_iter().collect(); // Some(())
let _: Option<()> = [Some(()), None, None].into_iter().collect(); // None](https://pbs.twimg.com/media/E-qpFiFXMAE8VTH.jpg?name=small&format=webp)
ALT // () items can be collected into a single (), since it carries no information: let _: () = [(), (), (), (), ()].into_iter().collect(); // This also works for Result<(), E>, collecting the Ok(()) into a single Ok(()): let _: Result<(), &str> = [Ok(()), Ok(()), Ok(())].into_iter().collect(); // Ok(()) let _: Result<(), &str> = [Ok(()), Err("?"), Err("??")].into_iter().collect(); // Err("?") // And for Option<()> too: let _: Option<()> = [Some(()), Some(()), Some(())].into_iter().collect(); // Some(()) let _: Option<()> = [Some(()), None, None].into_iter().collect(); // None
![// Strings can be collected into a single concatenated string:
let _: String = ["hello", " ", "world", "!"].into_iter().collect(); // "hello world!"
// This also works from and to Cow<str>:
let _: String = [Cow::Borrowed("hello "), Cow::Owned(123.to_string())].into_iter().collect(); // "hello 123"
let _: Cow<str> = ["hello", " ", "🐄"].into_iter().collect(); // "hello 🐄"
// OsStrs can be collected into an OsString:
let _: OsString = [OsStr::new("/dev"), OsStr::new("/i2c-"), OsStr::new("1")].into_iter().collect(); // "/dev/i2c-1"
// char codepoints can be encoded into an utf-8 String:
let zwj = '\u{200d}';
let _: String = ['👩', zwj, '❤', '\u{fe0f}', zwj, '💋', zwj, '👩'].into_iter().collect(); // "👩❤️💋👩"
// Paths are collected into a PathBuf by .join()'ing them as paths:
let _: PathBuf = ["/", "home", "mara", "rust", "library"].into_iter().collect(); // "/home/mara/rust/library"](https://pbs.twimg.com/media/E-qqNv0XEAECrif.jpg?name=small&format=webp)
ALT // Strings can be collected into a single concatenated string: let _: String = ["hello", " ", "world", "!"].into_iter().collect(); // "hello world!" // This also works from and to Cow<str>: let _: String = [Cow::Borrowed("hello "), Cow::Owned(123.to_string())].into_iter().collect(); // "hello 123" let _: Cow<str> = ["hello", " ", "🐄"].into_iter().collect(); // "hello 🐄" // OsStrs can be collected into an OsString: let _: OsString = [OsStr::new("/dev"), OsStr::new("/i2c-"), OsStr::new("1")].into_iter().collect(); // "/dev/i2c-1" // char codepoints can be encoded into an utf-8 String: let zwj = '\u{200d}'; let _: String = ['👩', zwj, '❤', '\u{fe0f}', zwj, '💋', zwj, '👩'].into_iter().collect(); // "👩❤️💋👩" // Paths are collected into a PathBuf by .join()'ing them as paths: let _: PathBuf = ["/", "home", "mara", "rust", "library"].into_iter().collect(); // "/home/mara/rust/library"






















