How I can use the on_message function in scope inside the main function and allow main to access everything inside the on_message function. Currently I cannot use the function inside the main function.
extern crate mosquitto_client as mosq;
use mosq::Mosquitto;
fn main() {
let m = on_connect();
let bonzo = m.subscribe("bonzo/#",0).expect("can't subscribe to bonzo");
let frodo = m.subscribe("frodo/#",0).expect("can't subscribe to frodo");
m.publish("bonzo/baggins", "hi".as_bytes(), 1, false).unwrap();
m.publish("frodo/#", "hi".as_bytes(), 1, false).unwrap();
on_message();
m.loop_until_disconnect(200).expect("broken loop");
}
fn on_connect() -> (mosq::Mosquitto) {
let m = Mosquitto::new("test");
m.connect("broker.hivemq.com",1883).expect("can't connect");
m
}
fn on_message() {
let mut mc = m.callbacks(());
mc.on_message(|_,msg| {
if ! msg.retained() {
if bonzo.matches(&msg) {
println!("bonzo {:?}",msg);
} else
if frodo.matches(&msg) {
println!("frodo {:?}",msg);
}
}
});
}
Please post your errors in a code block like this:
```
// your error goes here
```
You will need to give on_message access to the values. You can do this by passing them as an argument or by making on_message a function on a struct that has the bonzo and frodo variables in a field.
Compiling practise v0.1.0 (/home/pi/rust/practise)
error[E0308]: mismatched types
--> src/main.rs:13:24
|
13 | on_message(&mut m, &mut bonzo, &mut frodo);
| ^^^^^^^^^^ expected `str`, found struct `mosq::TopicMatcher`
|
= note: expected mutable reference `&mut str`
found mutable reference `&mut mosq::TopicMatcher<'_>`
error[E0308]: mismatched types
--> src/main.rs:13:36
|
13 | on_message(&mut m, &mut bonzo, &mut frodo);
| ^^^^^^^^^^ expected `str`, found struct `mosq::TopicMatcher`
|
= note: expected mutable reference `&mut str`
found mutable reference `&mut mosq::TopicMatcher<'_>`
error[E0277]: expected a `std::ops::Fn<(char,)>` closure, found `mosq::MosqMessage`
--> src/main.rs:28:30
|
28 | if bonzo.matches(&msg) {
| ^^^^ expected an `Fn<(char,)>` closure, found `mosq::MosqMessage`
|
= help: the trait `std::ops::Fn<(char,)>` is not implemented for `mosq::MosqMessage`
= note: required because of the requirements on the impl of `std::ops::FnOnce<(char,)>` for `&mosq::MosqMessage`
= note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `&mosq::MosqMessage`
error[E0308]: mismatched types
--> src/main.rs:28:16
|
28 | if bonzo.matches(&msg) {
| ^^^^^^^^^^^^^^^^^^^ expected `bool`, found struct `std::str::Matches`
|
= note: expected type `bool`
found struct `std::str::Matches<'_, &mosq::MosqMessage>`
error[E0277]: expected a `std::ops::Fn<(char,)>` closure, found `mosq::MosqMessage`
--> src/main.rs:31:30
|
31 | if frodo.matches(&msg) {
| ^^^^ expected an `Fn<(char,)>` closure, found `mosq::MosqMessage`
|
= help: the trait `std::ops::Fn<(char,)>` is not implemented for `mosq::MosqMessage`
= note: required because of the requirements on the impl of `std::ops::FnOnce<(char,)>` for `&mosq::MosqMessage`
= note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `&mosq::MosqMessage`
error[E0308]: mismatched types
--> src/main.rs:31:16
|
31 | if frodo.matches(&msg) {
| ^^^^^^^^^^^^^^^^^^^ expected `bool`, found struct `std::str::Matches`
|
= note: expected type `bool`
found struct `std::str::Matches<'_, &mosq::MosqMessage>`
error: aborting due to 6 previous errors
So how can fix these errors. What would be the correct types?
Compiling practise v0.1.0 (/home/pi/rust/practise)
error[E0502]: cannot borrow `m` as mutable because it is also borrowed as immutable
--> src/main.rs:13:16
|
7 | let mut bonzo = m.subscribe("bonzo/#",0).expect("can't subscribe to bonzo");
| - immutable borrow occurs here
...
13 | on_message(&mut m, &mut bonzo, &mut frodo);
| ^^^^^^ ---------- immutable borrow later used here
| |
| mutable borrow occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
error: could not compile `practise`.