From e44626f1b0bdc26d888408789eefb2b3603c163f Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Fri, 13 Nov 2026 14:28:46 -0600 Subject: fix: handle zero capacity in push --- learn_4/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'learn_4/src') 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 History { } 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::::new(0); + history0.push(11); + assert_eq!(history0.entries.len(), 0); + assert!(history0.pop().is_none()); } #[test] -- cgit v1.2.3