Help on Rust Course: FCC 26

“Task: Pass the test, by returning 24 from main, and type the return of the function with the type usize”

I’m having issues understanding the instructions for fcc 26 in the Rust course. I feel like there’s not enough information in the course so far to do this.

Do you know how to return values? Or is it the typing return values that is confusing you?

There are two things. First, what does the explicitly typed code have to do with anything. They’re introducing syntax that hasn’t been taught yet, specially “-> String” and “: String”. Second, my code isn’t working, and I’m getting mismatched type errors, which I’ll provide below.

fn main() {
  return 24
error[E0308]: mismatched types
 --> calculator/src/
2 | fn main() {
  |           - expected `()` because of default return type
3 |   24
  |   ^^ expected `()`, found integer


error[E0308]: mismatched types
  --> calculator/src/
11 |     assert_eq!(main(), 24);
   |     ^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found integer
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

They are teaching ‘typing’ of variables and return values.

This means that you did not provide a return type so the default return type was used.

But here you are actually returning an integer, which disagrees with the default type. The compiler doesn’t know if the return type is wrong or if the return value is wrong, so your code cannot be compiled.

So, you need to provide a type for the return value. This type goes after the list of arguments in the function signature for main(), following an arrow. Specifically, the return value needs to have the type usize.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.