|
1 | 1 | //! This module is about a queue implemented using a singly linked list |
2 | | -//! |
| 2 | +//! |
3 | 3 | //! The queue follows FIFO (First-In First-Out) principle |
4 | 4 | //! The [enqueue] method's time complexity is O(1) |
5 | 5 | //! The [dequeue] method's time complexity is O(1) |
@@ -68,17 +68,16 @@ where |
68 | 68 | T: Debug, |
69 | 69 | { |
70 | 70 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
71 | | - use std::fmt::Write as _; |
72 | | - |
73 | | - let mut output = String::from("LinkedListQueue ( elements: ["); |
74 | | - |
| 71 | + write!(f, "LinkedListQueue ( elements: [")?; |
| 72 | + let mut is_first = true; |
75 | 73 | for elem in self.iter() { |
76 | | - let _ = write!(output, " {elem:?} ").is_ok(); |
| 74 | + if !is_first { |
| 75 | + write!(f, ", ")?; |
| 76 | + } |
| 77 | + is_first = false; |
| 78 | + write!(f, "{:?}", elem)?; |
77 | 79 | } |
78 | | - |
79 | | - let _ = write!(output, "], length: {} )", self.len()).is_ok(); |
80 | | - |
81 | | - write!(f, "{output}") |
| 80 | + write!(f, "], length: {} )", self.len()) |
82 | 81 | } |
83 | 82 | } |
84 | 83 |
|
@@ -340,8 +339,6 @@ mod tests { |
340 | 339 | queue.enqueue(2); |
341 | 340 | queue.enqueue(3); |
342 | 341 |
|
343 | | - println!("{:?}", queue.len()); |
344 | | - |
345 | 342 | assert_eq!(queue.len(), 3); |
346 | 343 | } |
347 | 344 |
|
@@ -398,13 +395,11 @@ mod tests { |
398 | 395 | #[test] |
399 | 396 | fn test_get_from_queue() { |
400 | 397 | let mut queue = LinkedListQueue::new(); |
401 | | - |
402 | 398 | queue.enqueue(2); |
403 | 399 | queue.enqueue(3); |
404 | 400 | queue.enqueue(5); |
405 | 401 |
|
406 | 402 | let result = queue.get(1); |
407 | | - println!("{result:#?}", ); |
408 | 403 |
|
409 | 404 | assert!(result.is_some()); |
410 | 405 | } |
|
0 commit comments