Description
Consider this code:
fn f(a: ~[int]) {
match a {
~[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
_ => { fail!(); }
}
}
fn main() {
let a : ~[int] = ~[1i];
f(a);
}
When I attempt to compile it (rustc 0.9-pre (97cd495 2013-10-02 01:16:31 -0700)), I get an error like this:
% rustc /tmp/m.rs
/tmp/m.rs:3:8: 3:12 error: mismatched types: expected `~[int]` but found a ~-box pattern
/tmp/m.rs:3 ~[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
^~~~
error: aborting due to previous error
I do not find the message above useful. My main reaction is: "The whole pattern ~[b]
does look like a so-called ~-box pattern to me, which seems like it should fit the needs of a ~[int]
."
After inspecting the source code for _match.rs
, I think the issue is that I need to explicitly borrow a
and then match against &[b]
, like so:
fn f(a: ~[int]) {
match &a {
&[b] => { let b:int = b; println!("a: {:?} b: {:d}", a, b); }
_ => { fail!(); }
}
though I do not understand why the above compiles, since I would have thought that borrowing a ~[int]
via &a
would mean I should use match clauses of the form e.g. &~[b]
, not &[b]
.
In any case, we need a better error message here, preferably one that cross-references further discussion elsewhere, such as an appropriate (hypothetical) section of the rust.md manual. Note that the current rust.md documentation on match
does not suffice to explain what to do here.