LoginSignup
2
0

More than 5 years have passed since last update.

Rust, nomの使い方

Posted at

Rust, nomの使い方

検索して出てきたやつが古くて動かなかったのでメモ。
tag!は最初に指定した文字列(ここでは"Hello,")が先頭にあったとき、残りの文字列と分けて返す。

#[macro_use]
extern crate nom;

use nom::{IResult, space, alpha};

named!(parens, delimited!(char!('('), is_not!(")"), char!(')')));

named!(test1(&[u8]) -> &[u8], tag!("Hello,"));
named!(test2<&[u8], &[u8]>, tag!("Hello,"));
named!(test3, tag!("Hello,"));

fn test4(input: &[u8]) -> IResult<&[u8], &[u8]> {
    tag!(input, "Hello,")
}

// test1~4 are the same functionally

fn main() {
    let sample = "Hello, world!";

    let tes: &[u8] = &[0; 4];

    match test4(sample.as_bytes()) {
        IResult::Done(i, o) => {
            println!(
                "{}, {}",
                String::from_utf8(i.to_vec()).unwrap(), // world!
                String::from_utf8(o.to_vec()).unwrap(), // Hello, 
            )
        }
        _ => println!("Other!\n"),
    };
}

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0