Conversion of Haskell CBOR decoding code into Rust
I'd expect some kind of error type to describe what the problem was. There's at least two types of error I see, but the user cannot distinguish between them.
Rust does not perform tail-call optimization, so it's usually better to write things iteratively, especially when there's no fixed bound.
Using is_empty is more immediately obvious as to the desired behavior.
It seems very strange to read arbitrary integer lengths. Since the size is a usize, that means you could read an integer that takes 4 billion bytes or more! This is especially strange considering you can only return an i64.
Would encode the unit of size somehow. The easiest is to add _in_bytes to the argument name.
The vec! macro idiomatically uses square brackets to look like arrays.
There's no reason to allocate a vector here anyway; an array works fine.




use Value::*;

#[derive(Debug)]
enum Value {
    I(i64),
}

#[derive(Debug)]
enum Error {
    NotEnoughData,
    InvalidInteger,
}

fn read_int(mut buf: &amp;[u8], size_in_bytes: usize) -&gt; Result&lt;Value, Error&gt; {
    let mut acc = 0;

    for _ in 0..size_in_bytes {
        if buf.is_empty() { return Err(Error::NotEnoughData) }
        acc &lt;&lt;= 8;
        acc |= buf[0] as i64;
        buf = &amp;buf[1..];
    }

    Ok(I(acc))
}

fn cbor_decode(buf: &amp;[u8]) -&gt; Result&lt;Value, Error&gt; {
    match buf[0] {
        0x00 ... 0x17 =&gt; Ok(I(buf[0] as i64)),
        0x18 ... 0x1b =&gt; read_int(&amp;buf[1..], (buf[0] - 0x17) as usize),
        _             =&gt; Err(Error::InvalidInteger),
    }
}

fn main() {
    let x = [0x1a, 1, 0, 0];
    let y = cbor_decode(&amp;x);
    println!("{:?}", y);
}


A good programming practice is to not reimplement things for no reason. To that end, there's already crates for reading numbers out of byte slices:

extern crate byteorder;

use byteorder::{ByteOrder, BigEndian};

fn read_int(buf: &amp;[u8], size_in_bytes: usize) -&gt; Result&lt;Value, Error&gt; {
    if buf.len() &lt; size_in_bytes {
        Err(Error::NotEnoughData)
    } else {
        Ok(I(BigEndian::read_int(buf, size_in_bytes)))
    }
}


Even better, there's already crates for reading and writing CBOR data, such as serde_cbor. This plugs in to the de facto Rust serialization library, allowing you to operate at a higher level, defining structs that can be transformed to and from CBOR automatically.