Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Command-line scripting with Rust. Wait, what?!

Command-line scripting with Rust. Wait, what?!

More Decks by Андрей Листочкин (Andrey Listochkin)

Other Decks in Programming

Transcript

  1. #!/usr/bin/env cargo eval -- //! ```cargo //! [dependencies] //! time

    = "0.1.25" //! ``` extern crate time; fn main() { println!("{}", time::now().rfc822z()); }
  2. #!/usr/bin/env cargo eval -- //! ```cargo //! [package] //! edition

    = "2018" //! [dependencies] //! time = "0.1.25" //! ``` fn main() { println!("{}", time::now().rfc822z()); }
  3. #!/usr/bin/env cargo eval -- use std::io::*; fn main() -> Result<()>

    { for (n, line) in stdin().lock().lines().enumerate() { let line = line?; let line = line.trim_end(); if line.is_empty() { eprintln!("warning: empty line!"); } println!("{:>6}: {}", n, line.trim_end()) } Ok(()) }
  4. let s = "a̐éö̲\r\na\u{30a}"; let g: Vec<&str> = UnicodeSegmentation::graphemes(s, true)

    .collect(); let b = &["a̐", "é", "ö̲", "\r\n", "a\u{30a}"]; assert_eq!(g, b);
  5. lazy_static! { static ref RE: Regex = Regex::new( r"(?xu) #US

    date (?P<m> \d{2} ) # the month / (?P<d> \d{2} ) # the day / (?P<y> \d{4} ) # the year ", ) .unwrap(); } let before = "01/30/2018, 12/12/2019"; let after = RE.replace_all(before, "$y-$m-$d"); assert_eq!(after, "2018-01-30, 2019-12-12");