SHARE
TWEET

Untitled

a guest Feb 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use ::std::{
  2.   *,
  3.   iter::FromIterator,
  4. };
  5.  
  6. struct Key(usize, Box<str>);
  7.  
  8. const KEY: &str = "Key";
  9.  
  10. impl str::FromStr for Key {
  11.   type Err = ();
  12.  
  13.   fn from_str(s: &str) -> Result<Self, Self::Err>
  14.   {
  15.     let mut s = s.split("=");
  16.     match (s.next(), s.next()) {
  17.       (Some(key), Some(value)) if key.starts_with(KEY) => {
  18.         Ok(Key(key[KEY.len() ..].parse().map_err(mem::drop)?, value.into()))
  19.       },
  20.       _ => Err(()),
  21.     }
  22.   }
  23. }
  24.      
  25.  
  26. fn main() {
  27.     let input = "Key0=value,Key3=vvalue,Key2=vealue,Key10=vralue,Key4=vtalue,Key1=vyalue,Key17=vualue".to_owned();
  28.  
  29.     let mut kvs = Vec::
  30.       from_iter(
  31.         (0 .. 100).map(|_| None)
  32.       )
  33.     ;
  34.     input
  35.       .split(",")
  36.       .filter_map(|s| s.trim().parse().ok())
  37.       .for_each(|Key(id, value)| kvs[id] = Some(value))
  38.     ;
  39.  
  40.     println!("{:?}", itertools::join(kvs.iter().enumerate().filter_map(|(keyid,opt)|match opt{
  41.         None => None,
  42.         Some(value) => Some(format!("Key{}={}",keyid,value))
  43.     }),","));
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top