Date: April 17, 2026 Time: 50 minutes (Review 10 / Activity 30 / Reflection + Submit 10)
Get this instruction: https://github.com/ruhendrawan/cs1520_recitation
Work in pairs (coding + reflections).
Keep 3 tabs open:
By the end you should be able to:
HashMap
curlie
tester.sh
Last week:
filter
map
collect
sum
This week:
Vec
week14-rust-api
src/main.rs
src/util.rs
src/todos.rs
Run:
cd recitations/week14-rust-api cargo run
In another terminal:
bash tester.sh http://localhost:5000
Already in the starter:
GET /todos
POST /todos
GET /todos/{todo}
PUT /todos/{todo}
DELETE /todos/{todo}
POST /count
POST /mark
You will add:
GET /todos/by_due_date
#[derive(Serialize, Deserialize, Clone)] pub struct NewTodo { task: String, // add: due_date: Option<String> } #[derive(Serialize, Deserialize, Clone)] pub struct Todo { task: String, pub done: bool, // add the same optional field here too }
Work in src/util.rs.
Todo::new(...)
impl From<NewTodo> for Todo
Work in src/todos.rs and src/main.rs.
pub async fn get_sorted_by_due_date( State(_todo_list): State<SharedTodoList>, ) -> (StatusCode, Json<Vec<TodoWithId>>) { todo!("implement GET /todos/by_due_date") }
Also connect the route in main.rs:
main.rs
.route("/todos/by_due_date", get(todos::get_sorted_by_due_date))
No grammar check. Don't overcorrect. Answer these:
Submit your attendance in the google form: https://forms.gle/tYEtKjJunM1wb2we6
If you are stuck, come here later.
TodoWithId
Example response:
[ { "id": "todo2", "task": "finish slides", "done": false, "due_date": "2026-04-17" }, { "id": "todo4", "task": "submit lab", "done": false, "due_date": "2026-04-18" } ]
due_date
NewTodo
Todo
Todo::new
From<NewTodo> for Todo
POST
PUT
get_sorted_by_due_date()
let mut items: Vec<TodoWithId> = todo_list .iter() .map(|(id, todo)| TodoWithId { id: id.clone(), todo: todo.clone(), }) .collect();
Then sort the vector before returning it:
items.sort_by_key(|item| { // choose a key that puts earlier due dates first });
Create with a due date:
{ "task": "submit lab", "due_date": "2026-04-18" }
Update with a due date:
{ "task": "finish slides", "done": false, "due_date": "2026-04-17" }
If you use ISO strings like YYYY-MM-DD, simple string ordering matches date ordering.
YYYY-MM-DD
Use it to verify:
From<NewTodo>
None
Option<String>
2026-04-17