SHARE
TWEET

Untitled

a guest Aug 17th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pub struct Expr(pub Vec<Box<ExprItem>>);
  2.  
  3. pub enum ExprItem {
  4.     Word(String),
  5.     Candidates(Vec<Box<Expr>>),
  6. }
  7.  
  8. pub fn expand(expr: &Expr, idx: usize, stack: &mut Vec<String>, callback: &Fn(&mut Vec<String>))
  9. {
  10.     if idx == expr.0.len() {
  11.         callback(stack);
  12.         return;
  13.     }
  14.     let item: &ExprItem = &expr.0[idx];
  15.     match *item {
  16.         ExprItem::Word(ref val) => {
  17.             stack.push(val.to_owned());
  18.             expand(expr, idx + 1, stack, callback);
  19.             stack.pop();
  20.         }
  21.         ExprItem::Candidates(ref candidates) => {
  22.             for candidate in candidates {
  23.                 expand(&candidate, 0, stack, &|res| {
  24.                     expand(expr, idx + 1, res, callback);
  25.                 });
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. fn main() {
  32.     let expr = Expr(vec![
  33.         Box::new(ExprItem::Word("hello".to_owned())),
  34.         Box::new(ExprItem::Candidates(vec![
  35.             Box::new(
  36.                 Expr(vec![Box::new(ExprItem::Word("a".to_owned()))])
  37.             ),
  38.             Box::new(
  39.                 Expr(vec![Box::new(ExprItem::Word("b".to_owned()))])
  40.             ),
  41.         ])),
  42.         Box::new(ExprItem::Candidates(vec![
  43.             Box::new(
  44.                 Expr(vec![Box::new(ExprItem::Word("c".to_owned()))])
  45.             ),
  46.             Box::new(
  47.                 Expr(vec![Box::new(ExprItem::Word("d".to_owned()))])
  48.             ),
  49.         ])),
  50.     ]);
  51.     let mut stack = Vec::new();
  52.     expand(&expr, 0, &mut stack, &|res: &mut Vec<String>| {
  53.         println!("{}", res.join("-"));
  54.     });
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top