11mod store {
22 /// It's time to shift focus: our tickets are doing well, but they need a home.
3- /// A place where we can store them, search for them, and retrieve them.
3+ /// A place where we can store them, search for them, retrieve them.
44 ///
55 /// We can use many different data structures to store and manage our tickets.
66 /// JIRA users rely heavily on ticket identifiers, e.g. RUST-2018 or COVID-19.
7- /// It's a unique label that identifies univocally a single ticket, generally `<board name>-<ticket number>`.
7+ /// It's a unique label that unambiguously identifies a single ticket,
8+ /// generally `<board name>-<ticket number>`.
89 /// We don't have the concept of a board yet, so we'll roll with a simple numerical id.
910 ///
1011 /// What is the simplest data structure that allows us to fetch a ticket given its id?
1112 /// It makes sense for us to use a HashMap, also known as a dictionary in other languages.
12- /// You can read more about the HashMap in rust here: https://doc.rust-lang.org/std/collections/struct.HashMap.html
13+ /// You can read more about the HashMap in Rust here:
14+ /// https://doc.rust-lang.org/std/collections/struct.HashMap.html
1315 use std:: collections:: HashMap ;
1416 /// Let's import what we worked on in the previous set of exercises.
1517 use super :: recap:: Ticket ;
1618
1719 /// First we will create a TicketStore struct, with a `data` field of type HashMap.
1820 ///
19- /// HashMap is a *generic* struct: we need to specify two types, one for the key, and one for the stored value - HashMap<K, V>.
21+ /// HashMap is a *generic* struct: we need to specify two types, one for the key, and one for
22+ /// the stored value - HashMap<K, V>.
2023 ///
21- /// Let's set the value to the type of our Ticket, and we will use an unsigned integer for our identifier .
24+ /// Let's set the value type to our Ticket, and we will use an unsigned integer for our ids .
2225 struct TicketStore {
2326 /// The collection of stored tickets.
2427 data : HashMap < u32 , Ticket > ,
@@ -30,13 +33,13 @@ mod store {
3033 /// It can be invoked using `TicketStore::new()`.
3134 pub fn new ( ) -> TicketStore {
3235 TicketStore {
33- // Note that the compiler can infer the
34- // types for our HashMaps' key value pairs.
36+ // Note that the compiler can infer the types for our HashMaps' key-value pairs.
3537 data : HashMap :: new ( ) ,
3638 }
3739 }
3840
39- /// We take `&mut self` because we will have to mutate our HashMap to insert a new key/value pair.
41+ /// We take `&mut self` because we will have to mutate our HashMap to insert a new
42+ /// key-value pair.
4043 pub fn save ( & mut self , ticket : Ticket , id : u32 ) {
4144 todo ! ( )
4245 }
@@ -54,33 +57,39 @@ mod store {
5457
5558 /// Now let's put our TicketStore to use
5659 ///
57- /// We are going to create a ticket as we have previously done
58- /// then store it in our TicketStore, and finally validate that
59- /// the ticket we have saved in our store is indeed the same ticket
60+ /// We are going to create a ticket, save it in our TicketStore and finally validate that
61+ /// the ticket we have saved in our store is indeed the same ticket we created.
6062 #[ test]
6163 fn a_ticket_with_a_home ( ) {
6264 let ticket = generate_ticket ( Status :: ToDo ) ;
6365
64- // Pay special attention to the 'mut' keyword here: variables are immutable by default in Rust.
65- // The `mut` keyword is used to signal that you must pay special attention to the variable as it's likely to change later on
66- // in the function body.
66+ // Pay special attention to the 'mut' keyword here: variables are immutable
67+ // by default in Rust.
68+ // The `mut` keyword is used to signal that you must pay special attention to the
69+ // variable as it's likely to change later on in the function body.
6770 let mut store = TicketStore :: new ( ) ;
6871 let ticket_id = Faker . fake ( ) ;
6972
70- // Here we need to create a clone of our `ticket` because `save` takes the `ticket` argument as value,
71- // thus taking ownership of its value out of the caller function into the method.
72- // But we need `ticket`'s value after this method call, to verify it matches what we retrieve.
73- // Hence the need to clone it, creating a copy of the value and passing that copy to the `save` method.
73+ // Here we need to create a clone of our `ticket` because `save` takes the `ticket`
74+ // argument as value, thus taking ownership of its value out of the caller function
75+ // into the method.
76+ // But we need `ticket`'s value after this method call, to verify it matches what
77+ // we retrieve.
78+ // Hence the need to clone it, creating a copy of the value and passing that copy to
79+ // the `save` method.
7480 //
75- // (You might have to go back to the `recap` koan to derive a couple more traits for Ticket and Status...)
81+ // (You might have to go back to the `recap` koan to derive a couple more traits
82+ // for Ticket and Status...)
7683 store. save ( ticket. clone ( ) , ticket_id) ;
7784
7885 assert_eq ! ( store. get( & ticket_id) , & ticket) ;
7986 }
8087
81- /// We want our `get` method to panic when looking for an id to which there is no ticket associated (for now).
88+ /// We want our `get` method to panic when looking for an id to which there is no ticket
89+ /// associated (for now).
8290 ///
83- /// Rust has a way to handle this failure mode more gracefully, we will take a look at it later.
91+ /// Rust has a way to handle this failure mode more gracefully, we will take a look
92+ /// at it later.
8493 #[ test]
8594 #[ should_panic]
8695 fn a_missing_ticket ( ) {
0 commit comments