Reputation: 71
I have this function in Rust:
fn printboard(board: Vec<u32>) {
println!("| |{:>2$} {:>2$} {:>2$} {:>2$} {:>2$} {:>2$}| |",
board[1], board[2], board[3], board[4], board[5], board[6]);
}
For some reason, the code throws an error at board[3]
, saying 'expected usize, found u32.' This does not happen with any of the other board[x]
expressions. Any idea why this is happening?
Here is the full error:
error[E0308]: mismatched types
--> src/lib.rs:3:29
|
3 | board[1], board[2], board[3], board[4], board[5], board[6]);
| ^^^^^^^^ expected `usize`, found `u32`
|
= note: expected reference `&usize`
found reference `&u32`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
Upvotes: 6
Views: 268
Reputation: 60493
If each board element should be printed with a width of 2, you should omit the $
s:
fn printboard(board: Vec<u32>) {
println!("| |{:>2} {:>2} {:>2} {:>2} {:>2} {:>2}| |",
board[1], board[2], board[3], board[4], board[5], board[6]);
}
See Width in the std::fmt
docs.
The format specifier {:>2}
means to align right with a width of 2, while {:>2$}
means align right with a width specified by the 2nd argument (being board[3]
since its 0-indexed). This width argument is required to be a usize
, which is why you get the type error.
Upvotes: 6