| 1 | extern crate peg; |
| 2 | |
| 3 | peg::parser!(grammar borrows() for str { |
| 4 | use std::borrow::{ToOwned, Cow}; |
| 5 | |
| 6 | pub rule borrowed() -> &'input str |
| 7 | = $(['a'..='z']+) |
| 8 | |
| 9 | pub rule lifetime_parameter() -> Cow<'input, str> |
| 10 | = x:$(['a'..='z']+) { x.into() } |
| 11 | / "COW" { "cow".to_owned().into() } |
| 12 | }); |
| 13 | |
| 14 | use self::borrows::*; |
| 15 | |
| 16 | fn main() { |
| 17 | assert_eq!(borrowed("abcd"), Ok("abcd")); |
| 18 | assert_eq!(&*lifetime_parameter("abcd").unwrap(), "abcd"); |
| 19 | assert_eq!(&*lifetime_parameter("COW").unwrap(), "cow"); |
| 20 | } |