A vector can be destructured using pattern matching:
~~~~
-let numbers: [int, ..3] = [1, 2, 3];
+let numbers: &[int] = &[1, 2, 3];
let score = match numbers {
[] => 0,
[a] => a * 10,
}
not_useful
}
+ ty::ty_evec(_, ty::vstore_fixed(n)) => {
+ is_useful_specialized(cx, m, v, vec(n), n, left_ty)
+ }
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
let max_len = do m.rev_iter().fold(0) |max_len, r| {
match r[0].node {
else if true_found { Some(val(const_bool(false))) }
else { Some(val(const_bool(true))) }
}
+ ty::ty_evec(_, ty::vstore_fixed(n)) => {
+ let mut missing = true;
+ let mut wrong = false;
+ for r in m.iter() {
+ match r[0].node {
+ pat_vec(ref before, ref slice, ref after) => {
+ let count = before.len() + after.len();
+ if (count < n && slice.is_none()) || count > n {
+ wrong = true;
+ }
+ if count == n || (count < n && slice.is_some()) {
+ missing = false;
+ }
+ }
+ _ => {}
+ }
+ }
+ match (wrong, missing) {
+ (true, _) => Some(vec(n)), // should be compile-time error
+ (_, true) => Some(vec(n)),
+ _ => None
+ }
+ }
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
// Find the lengths and slices of all vector patterns.
}
pub fn main() {
- let x = [
+ let x = ~[
Foo { string: ~"foo" },
Foo { string: ~"bar" },
Foo { string: ~"baz" }
fn a() -> &[int] {
- let vec = [1, 2, 3, 4];
+ let vec = ~[1, 2, 3, 4];
let tail = match vec {
[_, ..tail] => tail, //~ ERROR does not live long enough
_ => fail!("a")
}
fn b() -> &[int] {
- let vec = [1, 2, 3, 4];
+ let vec = ~[1, 2, 3, 4];
let init = match vec {
[..init, _] => init, //~ ERROR does not live long enough
_ => fail!("b")
}
fn c() -> &[int] {
- let vec = [1, 2, 3, 4];
+ let vec = ~[1, 2, 3, 4];
let slice = match vec {
[_, ..slice, _] => slice, //~ ERROR does not live long enough
_ => fail!("c")
fn a() {
- let mut vec = [~1, ~2, ~3];
+ let mut vec = ~[~1, ~2, ~3];
match vec {
[~ref _a] => {
- vec[0] = ~4; //~ ERROR cannot assign to `vec[]` because it is borrowed
+ vec[0] = ~4; //~ ERROR cannot assign to `(*vec)[]` because it is borrowed
}
_ => fail!("foo")
}
}
fn b() {
- let mut vec = [~1, ~2, ~3];
+ let mut vec = ~[~1, ~2, ~3];
match vec {
[.._b] => {
- vec[0] = ~4; //~ ERROR cannot assign to `vec[]` because it is borrowed
+ vec[0] = ~4; //~ ERROR cannot assign to `(*vec)[]` because it is borrowed
}
}
}
fn c() {
- let mut vec = [~1, ~2, ~3];
+ let mut vec = ~[~1, ~2, ~3];
match vec {
[_a, .._b] => {
//~^ ERROR cannot move out
}
fn d() {
- let mut vec = [~1, ~2, ~3];
+ let mut vec = ~[~1, ~2, ~3];
match vec {
[.._a, _b] => {
//~^ ERROR cannot move out
}
fn e() {
- let mut vec = [~1, ~2, ~3];
+ let mut vec = ~[~1, ~2, ~3];
match vec {
[_a, _b, _c] => {}
_ => {}
fn a() -> &int {
- let vec = [1, 2, 3, 4];
+ let vec = ~[1, 2, 3, 4];
let tail = match vec {
[_a, ..tail] => &tail[0], //~ ERROR borrowed value does not live long enough
_ => fail!("foo")
--- /dev/null
+fn a() {
+ let v = [1, 2, 3];
+ match v {
+ [_, _, _] => {}
+ [_, _, _] => {} //~ ERROR unreachable pattern
+ }
+ match v {
+ [_, 1, _] => {}
+ [_, 1, _] => {} //~ ERROR unreachable pattern
+ _ => {}
+ }
+}
+
+fn main() {
+ a();
+}
_ => ()
}
- match [~"foo", ~"bar", ~"baz"] {
+ match ~[~"foo", ~"bar", ~"baz"] {
[a, _, _, .._] => { println(a); }
[~"foo", ~"bar", ~"baz", ~"foo", ~"bar"] => { } //~ ERROR unreachable pattern
_ => { }
}
- match ['a', 'b', 'c'] {
+ match ~['a', 'b', 'c'] {
['a', 'b', 'c', .._tail] => {}
['a', 'b', 'c'] => {} //~ ERROR unreachable pattern
_ => {}
--- /dev/null
+fn a() {
+ let x = [1, 2, 3];
+ match x {
+ [1, 2, 4] => ::std::util::unreachable(),
+ [0, 2, 3, .._] => ::std::util::unreachable(),
+ [0, .._, 3] => ::std::util::unreachable(),
+ [0, .._] => ::std::util::unreachable(),
+ [1, 2, 3] => (),
+ [_, _, _] => ::std::util::unreachable(),
+ }
+ match x {
+ [.._] => (),
+ }
+ match x {
+ [_, _, _, .._] => (),
+ }
+ match x {
+ [a, b, c] => {
+ assert_eq!(1, a);
+ assert_eq!(2, b);
+ assert_eq!(3, c);
+ }
+ }
+}
+
+pub fn main() {
+ a();
+}
fn a() {
- let x = [1];
+ let x = ~[1];
match x {
[_, _, _, _, _, .._] => ::std::util::unreachable(),
[.._, _, _, _, _] => ::std::util::unreachable(),
}
fn b() {
- let x = [1, 2, 3];
+ let x = ~[1, 2, 3];
match x {
[a, b, ..c] => {
assert_eq!(a, 1);
}
pub fn main() {
- let x = [
+ let x = ~[
Foo { string: ~"foo" },
Foo { string: ~"bar" },
Foo { string: ~"baz" }