O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercises

  1. combine is a parser framework that is similar to chomp. Translate the chomp examples to combine.

Solution: Here's the first example in combine:

extern crate combine;  use combine::{many, Parser}; use combine::char::{alpha_num, space};  fn main() {     let (x, y): (String, &str) =         many(alpha_num()              .or(space()))              .parse("String containing 123 non-alphanumerics").unwrap();     println!("{:?} {:?}", x, y); } 
  1. Pest is a PEG parser generator. Implement the ISO-8601 date standard (or parts of it) using Pest.

Solution: Here's a Pest parser that can read 2016-11-20T19:50:49+02:00:

impl_rdp! {     grammar! {         expression = { date ~ ["T"] ~ time ~ ["+"] ~ timezone }         date = { year ~ ["-"] ~ month ~ ["-"] ~ day }  time = { hour ~ [":"] ~ minute ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required