From b9143713e4911b4eec0fbcafb262b4212fa2c9da Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Wed, 3 Dec 2025 09:14:45 +0000 Subject: [PATCH] [Sync Iteration] rust/eliuds-eggs/2 --- solutions/rust/eliuds-eggs/2/Cargo.toml | 9 +++++++++ solutions/rust/eliuds-eggs/2/src/lib.rs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 solutions/rust/eliuds-eggs/2/Cargo.toml create mode 100644 solutions/rust/eliuds-eggs/2/src/lib.rs diff --git a/solutions/rust/eliuds-eggs/2/Cargo.toml b/solutions/rust/eliuds-eggs/2/Cargo.toml new file mode 100644 index 00000000..ae51b9c4 --- /dev/null +++ b/solutions/rust/eliuds-eggs/2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "eliuds_eggs" +version = "0.1.0" +edition = "2024" + +# Not all libraries from crates.io are available in Exercism's test runner. +# The full list of available libraries is here: +# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml +[dependencies] diff --git a/solutions/rust/eliuds-eggs/2/src/lib.rs b/solutions/rust/eliuds-eggs/2/src/lib.rs new file mode 100644 index 00000000..ae19a0aa --- /dev/null +++ b/solutions/rust/eliuds-eggs/2/src/lib.rs @@ -0,0 +1,16 @@ +pub fn egg_count(mut display_value: u32) -> usize { + let mut count = 0; + + // Loop continues until all set bits have been cleared + while display_value > 0 { + // Increment the count for the bit we are about to clear + count += 1; + + // This is Brian Kernighan's trick: + // display_value & (display_value - 1) clears the + // rightmost (least significant) '1' bit in display_value. + display_value &= display_value - 1; + } + + count +}