Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Read nom Input From Inside A Struct

Code

//! ```cargo
//! [dependencies]
//! nom = "7.1.3"
//! ```

use nom::character::complete::alphanumeric1;
use nom::character::complete::space1;
use nom::multi::separated_list1;
use nom::IResult;

struct Parser {
  source: Option<String>
}

impl Parser {
  pub fn parse(&self) -> IResult<&str, Vec<&str>> {
    let (source, result) = separated_list1(
      space1, alphanumeric1
    )(self.source.as_ref().unwrap().as_str())?;
    Ok((source, result))
  }
}


fn main() {
  let source = "the quick brown fox".to_string();
  let p = Parser {
    source: Some(source)
  };
  dbg!(p.parse().unwrap().1);

}

Results

[neopolitan_code_run:32] p.parse().unwrap().1 = [
    "the",
    "quick",
    "brown",
    "fox",
]