tutorial显示了一些非常基本的模式匹配示例,例如在整数上进行匹配以模拟c风格的switch语句.本教程还展示了如何对元组类型进行基本的解构,以及解构 struct .
似乎应该可以在向量上进行模式匹配,但我无法找到正确的语法,也没有找到任何例子.
例如,在Haskell中,您可以轻松地分解列表:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr func initValue [] = initValue
foldr func initValue (x:xs) = func initValue $ foldr initValue func xs
所以,看一个粗略的翻译,最好能做到:
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.
当前返回:
patterns.rs:10:4: 10:5 error: constant contains unimplemented expression type
patterns.rs:10 [] { ret initValue; }
^
error: aborting due to previous errors
本教程中有一个分解 struct (用{ .. }
定义)和元组(用( .. )
定义)的示例,因此考虑到向量还包含特殊语法(用[ .. ]
定义),似乎还应该内置对向量的支持.
如果我以错误的方式使用向量,请随时纠正我.