diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-13 14:28:46 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-13 14:28:46 -0600 |
| commit | e44626f1b0bdc26d888408789eefb2b3603c163f (patch) | |
| tree | 709f81440d16908668fbd97641b59d6df0a6f962 | |
| parent | 96e9806883d34fe60c433e84f201a751dee40a7a (diff) | |
fix: handle zero capacity in push
| -rw-r--r-- | learn_4/src/main.rs | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/learn_4/src/main.rs b/learn_4/src/main.rs index 4361b52..47408ef 100644 --- a/learn_4/src/main.rs +++ b/learn_4/src/main.rs @@ -12,6 +12,9 @@ impl<T> History<T> { } pub fn push(&mut self, value: T) { + if self.capacity == 0 { + return; + } if self.entries.len() == self.capacity { self.entries.remove(0); } @@ -63,6 +66,11 @@ mod tests { history.push(33); assert_eq!(history.entries.len(), 2); assert_eq!(history.entries.as_slice(), &[22, 33]); + + let mut history0 = History::<u32>::new(0); + history0.push(11); + assert_eq!(history0.entries.len(), 0); + assert!(history0.pop().is_none()); } #[test] |
