I doesn libreria standard Fornire questa funzionalità. Puoi scrivere il tuo con una 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"))
}
Il secondo argomento ingresso alla macro può essere un & str, char, o la chiusura/funzione appropriata. I tipi specificati devono implementare il tratto FromStr.
Si noti che ho messo insieme questo rapidamente in modo che non è stato testato a fondo.
fonte
2015-06-25 10:43:34
Sembra che non sia più necessario 'funzione (plugin)'? – Shepmaster
Ho aggiornato gli esempi alla versione corrente –