I'm trying to test out a simple couple functions which convert line endings in ASCII data from/to LF/CR. Despite what I'm trying my best to figure out, rustc gives me errors that leave me scratching my head.
const LF: u8 = 10;
const CR: u8 = 13;
fn lf_to_cr(s: &[u8]) -> Box<[u8]>
{
let mut outbytes = Box::from(s);
for i in 0..outbytes.len() { if outbytes[i] == LF { outbytes[i] = CR; } }
outbytes
}
fn cr_to_lf(s: &[u8]) -> Box<[u8]>
{
let mut outbytes = Box::from(s);
for i in 0..outbytes.len() { if outbytes[i] == CR { outbytes[i] = LF; } }
outbytes
}
The two errors I get seem odd, because len()
should tell Rust the returned value is usize
. However:
rustc 1.19.0-beta.2 (a175ee509 2017-06-15)
error: the type of this value must be known in this context
|
7 | for i in 0..outbytes.len() { if outbytes[i] == LF { outbytes[i] = CR; } }
| ^^^
error: the type of this value must be known in this context
|
14 | for i in 0..outbytes.len() { if outbytes[i] == CR { outbytes[i] = LF; } }
| ^^^
I did initially deref and parenthesize my array as (*outbytes)[i]
but I got the above error with CR
and LF
instead.