Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 13, 2026 02:42
Show Gist options
  • Select an option

  • Save rust-play/ea10f241c0acd365f7b2fcfc28897bc0 to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/ea10f241c0acd365f7b2fcfc28897bc0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::collections::HashMap;
/// Calculates Shannon Entropy for a slice of data.
/// H = -sum(p(x) * log2(p(x)))
fn calculate_entropy(data: &[&str]) -> f64 {
let len = data.len() as f64;
if len == 0.0 {
return 0.0;
}
// 1. Calculate frequencies of each unique element
let mut counts = HashMap::new();
for &item in data {
*counts.entry(item).or_insert(0) += 1;
}
// 2. Apply the Shannon Entropy formula
counts.values().fold(0.0, |acc, &count| {
let p_x = count as f64 / len;
// Entropy is usually measured in "bits" using log base 2
acc - (p_x * p_x.log2())
})
}
fn main() {
// Example 1: High certainty (Low entropy)
let certain_data = vec!["apple", "apple", "apple"];
// Example 2: High uncertainty (High entropy)
let uncertain_data = vec!["apple", "banana", "cherry"];
println!("Entropy (Certain): {:.4}", calculate_entropy(&certain_data));
println!("Entropy (Uncertain): {:.4}", calculate_entropy(&uncertain_data));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment