January 2019
Intermediate to advanced
520 pages
14h 32m
English
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 ...
Read now
Unlock full access