Oct 2, 2018
making a very simple not-at-all-robust base64 encode/decoder b64rs.
I started this project on an airplane and got further than I expected given that I couldn’t jump on Stackoverflow anytime I hit a bump. The rust compiler is so good and messages so helpful, that, along with examples and references in a local copy of the Rust Book, I could usually get something working.
Here are some things I’m learning.
The main purpose of lifetimes is to prevent dangling references to data. This is seems pretty straightforward in the chapter of The Rust Book, shown here:
{
let r;
{
let x = 5;
r = &x;
}
println!("r: {}", r);
}
The value for r
at the point println!
is called isn’t in scope, so won’t
compile.
These were a little less obvious and I needed to read up some more.
I hit this implementing a lookup table (HashMap
) of characters.
- temporary value dropped here while still borrowed
|
| temporary value does not live long enough
I already had a function that returned a vector of the 64 characters I used to encode.
pub fn encode_table() -> Vec<u8> {
(b'A'..=b'Z')
.chain(b'a'..=b'z')
.chain(0..=9)
.chain(vec![b'+', b'/'].into_iter())
.collect()
}
I wanted to use the return data to create table of chars to ints for decoding.
pub fn decode_table () {
let mut h: HashMap<&u8, &u8> = HashMap::new();
let t = encode_table().iter();
for (k, v) in t.zip(0..=64) {
h.insert(k, &v);
}
println!("h is {:?}", h);
}
Compiling throws a similar lifetime error.
Related tags: