Sqlite and hyper

Hello
I want to make a little webserver and connect that to an SQLite database.
I have a problem that's is I can't show my database data on the webpage.
this is my error:

error[E0507]: cannot move out of `email`, a captured variable in an `FnMut` closure
  --> src/main.rs:65:58
   |
61 |     let email: String = names[0].email.to_string();
   |         ----- captured outer variable
...
65 |         Ok::<_, Infallible>(service_fn(move |x| hello(x, email)))
   |                                                          ^^^^^ move occurs because `email` has type `std::string::String`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `email`, a captured variable in an `FnMut` closure
  --> src/main.rs:63:51
   |
61 |       let email: String = names[0].email.to_string();
   |           ----- captured outer variable
62 |       println!("{}", email);
63 |       let make_svc = make_service_fn(move |a| async {
   |  ___________________________________________________^
64 | |         // service_fn converts our function into a `Service`
65 | |         Ok::<_, Infallible>(service_fn(move |x| hello(x, email)))
   | |                                                          -----
   | |                                                          |
   | |                                                          move occurs because `email` has type `std::string::String`, which does not implement the `Copy` trait
   | |                                                          move occurs due to use in generator
66 | |     });
   | |_____^ move out of `email` occurs here

error: aborting due to 2 previous errors

help me
thank you:)

Try replacing hello(x, email) with hello(x, email.clone()).

thank you but not work :pensive:
new error:

error: captured variable cannot escape `FnMut` closure body
  --> src/main.rs:63:46
   |
63 |       let make_svc = make_service_fn(move |_a| async {
   |  ____________________________________________-_^
   | |                                            |
   | |                                            inferred to be a `FnMut` closure
64 | |         // service_fn converts our function into a `Service`
65 | |         Ok::<_, Infallible>(service_fn( |x| hello(x, email.clone())))
66 | |     });
   | |_____^ returns a reference to a captured variable which escapes the closure body
   |
   = note: `FnMut` closures only have access to their captured variables while they are executing...
   = note: ...therefore, they cannot allow references to captured variables to escape

You have to create copy:

let email = ...;
let email2 = email.clone();
capture(move |...| email);
capture2(move |...| email2);

You have the problem twice because you have 2 closures...

clone it in the outer closure and then move the clone in the service_fn closure.

thank you :pray:.
a friend change my code to :
use std::sync::{Arc, Mutex};
let xa = "Test".to_string();
let email = Arc::new(Mutex::new(xa));

let make_svc = make_service_fn(move |_| {
    let _email3 = email.clone();

    async move {
        let _email4 = _email3.clone();
        let rrr = "hello";
        let text = rrr.to_string();
        Ok::<_, Infallible>(service_fn(move |x| hello(x, text.clone())))
    }
});

Hmm, is this the complete version of your hyper service? In this example in any case it looks like _email3 and _email4 are unused, in which case you can just remove them.

no, it's just a test code
:slightly_smiling_face: :ok_hand:

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