Rust pattern matching over a vector

ash picture ash · Feb 14, 2012 · Viewed 18.1k times · Source

The tutorial shows some very basic examples of pattern matching, such as matching over an integer to emulate a c-style switch statement. The tutorial also shows how to do basic destructuring over a tuple type, and destructuring structures.

It seems like it should be possible to pattern match over a vector but I cannot figure out the right syntax for it and I haven't found any examples of it.

For example, in Haskell you can easily destructure a list:

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr func initValue []     = initValue
foldr func initValue (x:xs) = func initValue $ foldr initValue func xs

So, looking at a rough translation, it would be nice to be able to do:

fn foldr<A, B>(func: fn(A, B) -> B,
               initValue: B,
               vals: [A]) -> B {
  alt vals {
    [] { ret initValue; }
    _  {
      let h = vec::head(vals),
          t = vec::tail(vals);
      ret foldr(func, func(initValue, h), t);
    }
  }
}

Note: I know you could use an if statement here, I am just using this as an example of pattern matching over a vector.

This currently returns:

patterns.rs:10:4: 10:5 error: constant contains unimplemented expression type
patterns.rs:10     [] { ret initValue; }
                ^
error: aborting due to previous errors

There is an example in the tutorial for destructuring structures (defined with { .. }) and tuples (defined with ( .. )), so it seems like there should be built-in support for vectors as well considering they also contain a special syntax (defined with [ .. ]).

Feel free to correct me if I am using vectors in the wrong way as well.

Answer

stralep picture stralep · Jul 15, 2019

You need slice patterns:

fn vec_alt<T>(vals: Vec<T>) -> &'static str {
    match vals[..] {
        [a, b] => "two elements",
        [a, b, c] => "three elements",
        _ => "otherwise",
    }
}