Is &Vec the same as &[u8]

⚓ rust    📅 2025-06-05    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Is &Vec the same as &[u8]

I recognized you can use &Vec alsmost everywhere where a &[u8] is needed. For testing, I wrote this code:

fn main() {
    let a = vec![9_u8, 1, 2];
    let b: &[u8] = &a;
    let c: &Vec<u8> = &a;

    if b == c {
        println!("test1");
    }

    assert_same_type(b, c);
}


fn assert_same_type<T: ?Sized>(_: &T, _: &T) {
    println!{"test2"};
}

"test1" and "test2" are printed without any trouble. Are &[u8] and &Vec the same internally or is it just converted (like &String to &str)? I thought it could not be the same, because &[u8] is a slice of an array and &Vec of an Vector. But the test looks like I was wrong.

5 posts - 4 participants

Read full topic

🏷️ rust_feed