rust - unique vector patterns are no longer supported -
i realize rust in flux, i'm trying learn anyway. i'm trying understand how adapt following example, works 0.9, similar works 0.10:
fn main() { let argv = std::os::args(); let (first, last) = match argv { [_, first_arg, .., last_arg] => (first_arg, last_arg), _ => fail!("error: @ least 2 arguments expected.") }; println!("the first argument {:s}, \ , last argument {:s}.", first, last); } when build 0.10, following error:
error: couldn't read test.rc: no such file or directory (no such file or directory) orflongpmacx8:rust pohl_longsine$ rustc test.rs test.rs:9:9: 9:37 error: unique vector patterns no longer supported test.rs:9 [_, first_arg, .., last_arg] => (first_arg, last_arg), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due previous error my question: still possible use pattern matching on argv, different syntax, or using match statement on argv no longer possible @ all? if it's former, have change?
you can still match on slices &[t] , fixed length arrays [t, .. n]. so, in case,
fn main() { let argv = std::os::args(); let (first, last) = match argv.as_slice() { [_, ref first_arg, .., ref last_arg] => (first_arg, last_arg), _ => fail!("error: @ least 2 arguments expected.") }; println!("the first argument {:s}, \ , last argument {:s}.", *first, *last); } note addition of refs. argv ~[~str], i.e. contents owned strings ~str, move ownership when passed value [_, first_arg, .., last_arg] pattern do. it's illegal move ownership out behind borrowed pointer (like slice &[~str]) pattern illegal. 1 can borrow slice (and other pattern) using ref keyword, making first , last both references of type &~str.
one might wonder why *first , *last dereferences aren't trying move ~str out behind &~str, it's because println! macro expanding &*first , &*last, fine.
(we write => (first_arg.as_slice(), last_arg.as_slice()) borrow 2 &~strs straight string slices &str, means don't need dereference in println!.)
Comments
Post a Comment