Can't get pest to compile

I'm using Rust 2018 and trying to get the example from the pest website to work.
The ident.pest file is:

alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
ident = { (alpha | digit)+ }
ident_list = _{ !digit ~ ident ~ (" " ~ ident)+ }

My cargo.toml is:

[package]
name = "pesttest"
version = "0.1.0"
authors = ["Mark"]
edition = "2018"
[dependencies]
pest = "2"
pest_derive = "2"

And main.rs is:

use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "ident.pest"]
pub struct IdentParser;

fn main() {
    let pairs = IdentParser::parse(Rule::ident_list, "a1 b2").unwrap_or_else(|e| panic!("{}", e));

    for pair in pairs {
        println!("Rule:    {:?}", pair.as_rule());
        println!("Span:    {:?}", pair.as_span());
        println!("Text:    {}", pair.as_str());
        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::alpha => println!("Letter:  {}", inner_pair.as_str()),
                Rule::digit => println!("Digit:   {}", inner_pair.as_str()),
                _ => unreachable!()
            };
        }
    }
}

The errors begin are:

error[E0599]: no function or associated item named `parse` found for type `IdentParser` in the current scope
 --> src/main.rs:8:30
  |
5 | pub struct IdentParser;
  | ----------------------- function or associated item `parse` not found for this
...
8 |     let pairs = IdentParser::parse(Rule::ident_list, "a1 b2").unwrap_or_else(|e| panic!("{}", e));
  |                              ^^^^^ function or associated item not found in `IdentParser`

I'm guessing that the problem is a simple one to do with the switch from [#use_macros] in Rust 2015 to use in 2018, but clearly I can't see what to do.

The problem is parse() is part of the pest::Parser trait and you haven't imported it. try adding use pest::Parser to the top of main.rs.

Thank you that worked!
(I thought I'd tried it too, but obviously hadn't.)

1 Like