Crate range_parser

source ·
Expand description

§Range Parser

range-parser is a simple Rust crate to parse range from text representation (e.g. 1-3,5-8, 1,3,4, 1-5) into a Vector containing all the items for that range.

§Get started

First add range-parser to your Cargo.toml:

[dependencies]
range-parser = "0.1.2"

Then parse a range from a string:

let range_str = "1-3,5-8";
let range: Vec<u64> = range_parser::parse(range_str).unwrap();
assert_eq!(&range, &[1, 2, 3, 5, 6, 7, 8]);

§Examples

§Parse a range with a dash

let range: Vec<u64> = range_parser::parse("1-3").unwrap();
assert_eq!(range, vec![1, 2, 3]);

§Parse a range with commas

let range: Vec<u64> = range_parser::parse("1,3,4").unwrap();
assert_eq!(range, vec![1, 3, 4]);

§Parse a mixed range

let range: Vec<u64> = range_parser::parse("1,3-5,2").unwrap();
assert_eq!(range, vec![1, 3, 4, 5, 2]);

§Parse a range with negative numbers

let range: Vec<i32> = range_parser::parse("-8,-5--1,0-3,-1").unwrap();
assert_eq!(range, vec![-8, -5, -4, -3, -2, -1, 0, 1, 2, 3, -1]);

§Parse a range with custom separators

let range: Vec<i32> = range_parser::parse_with("-2;0..3;-1;7", ";", "..").unwrap();
assert_eq!(range, vec![-2, 0, 1, 2, 3, -1, 7]);

Enums§

Traits§

  • A trait for types that have a unit value.

Functions§

  • Parse a range string to a vector of any kind of number
  • Parse a range string to a vector of any kind of numbers with custom separators

Type Aliases§