Passing data into our views
Passing data into a Rocket application is fairly straightforward. To do this, we will be altering part of the to_do
module in the src/json_serialization/to_do_item.rs
file:
- First, we must define our dependencies with the following code:
use serde::Deserialize; use serde::Serialize;
- We use the
serde
crate to allow our struct to serialize and deserialize the data. This allows the data to be passed into the view or returned from the view. Now, we will add aSerialize
trait to ourToDoItem
struct, as follows:#[derive(Deserialize, Serialize)] pub struct ToDoItem { pub title: String, pub status: String }
- Now that our struct has been altered, we can build a simple view that merely takes in the JSON body and returns it. In our
src/main.rs
file, we will define the following dependency:use crate::json_serialization::to_do_item::ToDoItem;
- Now that our dependency has been defined, we can create our...