rustcompiler-errorstype-mismatchwinit

Declared function return does not match the type of the value that is returned (and inferred by the compiler)


I'm trying to get started with [imgui ] for Rust (along with [glutin] and [winit]), and I'm working on creating the event loop ([glutin]). To do this, I'm calling event_loop.run_return() (not run() since I want it to return control once the UI is closed).

According to the docs (Ctrl+Q in CLion), the return type of run_return() is i32 (an integer):

winit::platform::run_return impl<T> EventLoopExtRunReturn for EventLoop<T>
fn run_return<F>(&mut self, event_handler: F) -> i32 where     F: FnMut(Event<'_, Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow),
Initializes the winit event loop.
...

When I look at the source, it show the exact same (an i32):

fn run_return<F>(&mut self, event_handler: F) -> i32

I want to log what this returns (I'm using [tracing]), so I tried to store it as a variable and then print it:

let exit_code = ui_system
    .event_loop
    .run_return(event_loop_run);

drop(_run_span_entered); //Ignore, tracing

info!("exit_code: {exit_code}");

This errors on the info!(), because () doesn't implement std::fmt::Display() doesn't impolement std::fmt::Display image

This is pointing at {exit_code}, which is most definitely not (). Also note how CLion infers the type of exit_code to be i32.

If I try to force the type of exit_code, then the info!() is happy, but now the variable definition errors out:

mismatched types [E0308] expected `i32`, found `()`

mismatched types i32/()

Unless I'm going crazy, this is clearly wrong, because the function definition says it must return an i32, but yet the compiler somehow thinks it isn't?

cargo build shows the same thing:

error[E0308]: mismatched types                                                                                                                                                                                                                                                                                         
  --> src\main.rs:60:25
   |
60 |       let exit_code:i32 = ui_system
   |  ___________________---___^
   | |                   |
   | |                   expected due to this
61 | |         .event_loop
62 | |         .run_return(event_loop_run);
   | |___________________________________^ expected `i32`, found `()`


Solution

  • In the previous version of winit, run_return() did return ().

    I suspect that your Cargo.toml specifies 0.26 (or earlier) but CLion is somehow incorrectly presenting you with documentation for 0.27.