PUT – Updating data

Once the data is saved, we might want to provide the ability to modify it. This is a task for the PUT method. Use this method to handle changes to the data:

(&Method::PUT, Some(id)) => {
if let Some(user) = users.get_mut(id) {
*user = UserData;
response_with_code(StatusCode::OK)
} else {
response_with_code(StatusCode::NOT_FOUND)
}
},

This code tries to find a user instance in the user database with the get_mut method. This returns a mutable reference wrapped with either a Some option, or a None option if the corresponding value isn't found. We can use a dereference operator, *, to replace the data in the storage.

If the user's data was found and replaced, the branch returns an OK status code. If there's no user with the requested ID, the branch returns NOT_FOUND

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.187