What is the difference between usize and u32 in Rust? When to use which?
Hello everyone! I started learning Rust a couple of weeks ago and encountered something I don't understand. In The Rust Programming Language book, I often see the `usize` type used for indices and lengths, but I also encounter `u32`. I can't figure out the fundamental difference between them.
For example, I wrote a simple program that iterates over vector elements:
```rust
fn main() {
let v = vec![10, 20, 30, 40, 50];
// Option 1: using u32
for i in 0..v.len() as u32 {
println!("{}", v[i as usize]);
}
// Option 2: using usize
for i in 0..v.len() {
println!("{}", v[i]);
}
}
```
The first option works, but I have to do a lot of type casting: `as u32` and `as usize`. The second option works without any casting. But I've heard that
No answers yet.
Log in to leave an answer