Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created April 5, 2026 21:31
Show Gist options
  • Select an option

  • Save raphlinus/0567bb617724a65db24ba7242af4026a to your computer and use it in GitHub Desktop.

Select an option

Save raphlinus/0567bb617724a65db24ba7242af4026a to your computer and use it in GitHub Desktop.
Example of AI-generated docstring
/// A structure representing a system that tracks "foozles".
pub struct FoozleTracker {
/// The current count of foozles observed.
/// We use u64 to provide a massive range (0 to 18,446,744,073,709,551,615),
/// minimizing the risk of counter overflow in most practical applications.
pub foozle_count: u64,
}
impl FoozleTracker {
/// Creates a new FoozleTracker starting at zero.
pub fn new() -> Self {
Self { foozle_count: 0 }
}
/// Increments the `foozle_count` by exactly one.
///
/// ### Correctness Argument
/// This implementation is correct because:
/// 1. **Exclusive Access**: By taking `&mut self`, Rust's borrow checker guarantees
/// that this function has exclusive, mutable access to the struct. No other
/// thread or function can read or write to `foozle_count` while this is executing.
/// 2. **In-place Mutation**: It uses the `+=` operator, which is the idiomatic
/// way to perform addition and assignment in Rust, ensuring the value is
/// updated directly in memory without unnecessary allocations.
/// 3. **Type Safety**: The use of `u64` prevents negative counts, which
/// logically matches the concept of a "count."
pub fn increment_foozle_count(&mut self) {
self.foozle_count += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_increment_logic() {
// Initialize the tracker
let mut tracker = FoozleTracker::new();
// Assert initial state is correct
assert_eq!(tracker.foozle_count, 0, "Tracker should start at zero.");
// Perform the action
tracker.increment_foozle_count();
// Verify the result
// This test proves that the function correctly modifies the internal state
// and that the increment is exactly 1.
assert_eq!(
tracker.foozle_count, 1,
"The count should be exactly 1 after a single increment."
);
// Verify multiple increments to ensure consistency
for i in 1..10 {
tracker.increment_foozle_count();
assert_eq!(
tracker.foozle_count, (i + 1) as u64,
"The count failed to increment correctly at iteration {}", i
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment