49

This is my first day with Rust, but I'm trying to do something trivial, and I'm stuck.

What I'm trying to do is to add an struct to a Vector, and return the result. What I'm trying is to create a very simple REST service which will store the data in memory when posting, and return all the data in JSON format when doing a GET.

This is my current code:

fn main() {
    let mut server = Nickel::new();
    let mut reservations = Vec::new();

   server.post("/reservations/", middleware! { |request, response|
        let reservation = request.json_as::<Reservation>().unwrap();

        reservations.push(reservation); // <-- error occurs here

        format!("Hello {} {}", reservation.name, reservation.email)

    });

    server.listen("127.0.0.1:3000");
}

I tried this solution with a RefCell, but then I get the error that the trait Sync is not implemented for Vec<reservation::Reservation>

Community
  • 1
  • 1
Erik Pragt
  • 13,513
  • 11
  • 58
  • 64

1 Answers1

67

This is a very good example of how Rust protects you from thread unsafety.

If you think about it, in your current code it would be possible that multiple threads try to concurrently mutate reservations without any kind of synchronization. This is a data race and Rust will complain about it.

A possible solution would be to wrap the reservations vector into a Mutex to get synchronization. You will also need an Arc (atomic reference counting), since Rust cannot prove that reservations will live longer than the threads.

With these changes, your code should be like the following:

use std::sync::{Arc, Mutex};
fn main() {
    let mut server = Nickel::new();
    let reservations = Arc::new(Mutex::new(Vec::new()));

    server.post("/reservations/", middleware! { |request, response|
        let reservation = request.json_as::<Reservation>().unwrap();

        reservations.lock().unwrap().push(reservation); // <-- error occurs here

        format!("Hello {} {}", reservation.name, reservation.email)

    });

    server.listen("127.0.0.1:3000");
}

You can check the documentation for additional info about Mutex and Arc.

Veedrac
  • 58,273
  • 15
  • 112
  • 169
aochagavia
  • 5,887
  • 5
  • 34
  • 53
  • 3
    favorite post ever, thank you very much Erik Pragt for the question and @aochagavia for the answer. – Hassek Mar 06 '19 at 19:28
  • 4
    "this is a good example of how Rust forces you to write inefficient, verobose and complicated code for something that will only ever run on a single thread" (in many cases) – Dominic Jan 24 '22 at 17:46
  • 2
    @Dominic Precisely: I'm a Rust-newbie writing a single-threaded app and am beating my head against the wall over this issue. – tufelkinder Jun 07 '22 at 16:16