The standard library does not provide this function. You can write your own macro.
macro_rules! scan { ( $string:expr, $sep:expr, $( $x:ty ),+ ) => {{ let mut iter = $string.split($sep); ($(iter.next().and_then(|word| word.parse::<$x>().ok()),)*) }} } fn main() { let output = scan!("2 false fox", char::is_whitespace, u8, bool, String); println!("{:?}", output); // (Some(2), Some(false), Some("fox")) }
The second input argument to the macro can be a & str, char or the corresponding closing function /. The specified types must implement the FromStr property.
Please note that I put this together quickly so that it is not fully tested.
Ab
source share