Rust: Empty struct in empty vec: capacity == usize max

Created on 26 Nov 2017  路  4Comments  路  Source: rust-lang/rust

struct Foo {}
let mut vec: Vec<Foo> = Vec::new();
println!("{}", vec.capacity());

output: 18446744073709551615

Most helpful comment

Hi @yakoshk, is there any reason this should be an issue? The struct Foo in this case has size zero, and it is expected that the Vec behaves like this with such elements. It uses no allocation and can store as many elements as fits in its capacity counter.

All 4 comments

Hi @yakoshk, is there any reason this should be an issue? The struct Foo in this case has size zero, and it is expected that the Vec behaves like this with such elements. It uses no allocation and can store as many elements as fits in its capacity counter.

To be clear, though, vec.len() should be 0 for an empty Vec, regardless of the type.

@mark-i-m Was that perhaps a mistype? There's no mention of vec.len() here.

@bstrie Yes, but I thought maybe there might have been confusion between capacity and len.

Was this page helpful?
0 / 5 - 0 ratings