I am trying to write code that repeatedly splits a slice into smaller pieces based on the data in the splice, as below:
fn my_fn(data: &'a [u8]) -> ... { // say data is 100 bytes long
let (foo, data) = data.split_at(4); // now the last 96 bytes alone are in data
let (bar, data) = data.split_at(2); // now data has 94 bytes, etc.
if some_cond(foo, bar) {
let (baz, data) = data.split_at(6);
// ... use baz ...
}
data // return what's left of the original slice named data
}
The above is not right, because the let essentially re-declares the variable data. So, rustc correctly points out that the variable data inside the some_cond check is not being used. However, when I remove the 'let', I get the error:
left-hand of expression not valid
The usage examples of split_at invariably use 'let'.
I am getting around this by doing:
let mut data = data;
if some_cond(foo, bar) {
let (baz, data1) = data.split_at(6);
data = data1;
// ... use baz ...
}
fn my_fn(mut data: &[u8]) -> &[u8] { // say data is 100 bytes long
let foo = data.bite(4); // now the last 96 bytes alone are in data
let bar = data.bite(2); // now data has 94 bytes, etc.
if some_cond(foo, bar) {
let baz = data.bite(6);
println!("{:?}", baz);
}
data // return what's left of the original slice named data
}