Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "expected reference `&usize`" when printing?

Tags:

rust

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)
like image 619
Whirlagon Avatar asked Sep 08 '21 00:09

Whirlagon


People also ask

Does reference check mean job offer?

A reference check typically means a hiring manager is near-ready to extend an offer to a candidate, and they want one final confirmation that you are the right fit for their team, Foss says.

What does it mean when someone puts you as their reference?

Definition & Example of a Character Reference A character reference is a recommendation provided by someone who knows you personally and can describe your attributes and traits. Find out how a character reference works, what it includes, and how to get one—because you'll likely need one at some point in your life.

Can you get rejected after reference check?

It is possible to get rejected after a reference check. In fact, some sources say that candidates get rejected about 10 – 20% of the time after a reference check. Most often, a candidate will be rejected due to providing fake references that are discovered when they're vetted.

How many candidates get reference checks?

Most employers will call your references only if you are the final candidate or one of the final two. Occasionally the final three or four. Every now and then an employer will check all the people they interview, although to me that's inconsiderate of the reference.

What is referencing and why is it important?

Referencing is an important part of academic work. It puts your work in context, demonstrates the breadth and depth of your research, and acknowledges other people’s work. You should reference whenever you use someone else’s idea. Introduction to referencing at Leeds

Why do employers take up references?

Taking up references may be one way in which the employer is seeking to find the answers to the questions they have. However, if your references do not provide the answers that the employer needs, they are likely to continue looking at other candidates.

Why is reference range important in testing?

The reason a test is performed sometimes guides which reference range is used to interpret results and guide treatment decisions.

What is referencing in an assignment?

Referencing is the process of mentioning the sources that you obtained your information from. Below are some of the important reasons that you should reference your assignment:


Video Answer


1 Answers

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.

like image 101
kmdreko Avatar answered Nov 15 '22 07:11

kmdreko