Sure, there is some syntax, though it is not as explicit on lifetimes as you mean and in fact it is more general. By doing match
as you do you are actually moving x
into the match, so it is consumed. No wonder that z.slice(...)
can't escape the block. But you can avoid moving x
into the match by using reference patterns (note the ref
before z
):
let default: &[int] = [];
let x = Bar(vec![1, 2, 3]);
let y = match x {
Bar(ref z) => z.slice(0, z.len()),
Other => default
};
println!("{}", y);
This way x
is not moved into match block - its internals are borrowed instead. This program compiles and prints out [1, 2, 3]
.