As per what I stumbled into (despite being somewhat experienced) this
match x {
..y => smaller,
y => equal,
_ => greater
}
is ambiguous and thus a beginner’s trap: does y
want to capture something (not possible in the 1st branch) or be an outer variable? Despite the limitation that ranges can’t capture like this, Rust has chosen variable names in patterns to mean capturing. This makes it syntactically impossible to pass in dynamic values.
Leaving aside that dynamic branches might not optimize as well as compile time known branches, I find them very desirable. Since one syntax can’t have it both ways, my idea is to use block expressions to disambiguate them. Here y
would clearly be an outer variable:
match x {
..{y} => smaller,
{y} => equal,
_ => greater
}
And since the range branch isn’t actually ambiguous, a block may not be deemed necessary there.