How to modify function because of some mismatched type errors?

Hi

How do I modify the following function because I am getting some mismatched type errors.

fn on_connect() -> (&'static str, &'static str, &'static str, &'static str, &'static str) {
    let m = Mosquitto::new("slm");
    
    let conf = Ini::load_from_file("conf.ini").unwrap();
    
    let mqtt = conf.section(Some("Mqtt")).unwrap();
    let broker = mqtt.get("broker_ip").unwrap();
    let connect = m.connect(broker, 1883).expect("Failed to connect to broker");
    (m, conf, mqtt, broker, connect)
}     
pi@raspberrypi:~/rust/slm_controller $ cargo run
   Compiling slm_controller v0.1.0 (/home/pi/rust/slm_controller)
warning: unused import: `serde_json::json`
 --> src/main.rs:5:5
  |
5 | use serde_json::json;
  |     ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `std::collections::HashMap`
 --> src/main.rs:6:5
  |
6 | use std::collections::HashMap;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
  --> src/main.rs:16:6
   |
16 |     (m, conf, mqtt, broker, connect)
   |      ^ expected `&str`, found struct `mosq::Mosquitto`

error[E0308]: mismatched types
  --> src/main.rs:16:9
   |
16 |     (m, conf, mqtt, broker, connect)
   |         ^^^^ expected `&str`, found struct `ini::Ini`

error[E0308]: mismatched types
  --> src/main.rs:16:15
   |
16 |     (m, conf, mqtt, broker, connect)
   |               ^^^^ expected `str`, found struct `ini::ini::Properties`
   |
   = note: expected reference `&'static str`
              found reference `&ini::ini::Properties`

error[E0308]: mismatched types
  --> src/main.rs:16:29
   |
16 |     (m, conf, mqtt, broker, connect)
   |                             ^^^^^^^ expected `&str`, found `()`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `slm_controller`.

The link for the mosquitto client library is as follows because I am using the mosquitto client as the mqtt: GitHub - stevedonovan/mosquitto-client

Thanks

One of the things you return in the tuple at the end of your function is of type Mosquito, however, your function signature says you are returning &str. You are also returning other things that aren't &str, same reasoning applies.

So you need to bring the two in accordance. Which way really depends on what you actually intended to do...

Ok how do i do this?

Well, what do you want to do? Do you want your method to return strings or the other types?

If you want to return these other types, you need to change the method signature from:

fn on_connect() -> (&'static str, &'static str, &'static str, &'static str, &'static str)

to:

fn on_connect() -> (mosq::Mosquitto, ini::Ini, ini::ini::Properties, &'static str)

The last one in the tuple (connect) is actually an empty tuple. You probably don't want to return that.

If you want to return strings, you will have to make a string representation of your variables. I have no way of telling what would make sense here for you, but most types implement Debug which lets you turn them into a string like:

let i_impl_debug = SomeType::new();
let string = format!( "{:?}", &i_impl_debug );

I do have the impression that this question is more about programming basics than about rust. I warn you that Rust is a difficult first language. It's not to say that it's impossible, but you will sweat it. It might be much more fluid to learn another language first.

There are also books and courses that will just teach you programming basics, in a more language-agnostic way that might help you prepare before taking on Rust.

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