Rust Examples

From PeformIQ Wiki
Jump to navigation Jump to search
// src/lib.rs (Rust Service)
use std::time::Instant;
use sha2::{Sha256, Digest};

// Simulating the same heavy, synchronous transformation/hashing operation
pub fn calculate_user_partition_key(user_id: &str, complexity: u32) -> String {
    let start = Instant::now();
    let mut base = user_id.to_string();

    for i in 0..complexity {
        let mut hasher = Sha256::new();
        // Rust's zero-cost abstractions and memory safety shine here
        hasher.update(base.as_bytes());
        hasher.update(i.to_string().as_bytes());
        base = format!("{:x}", hasher.finalize());
    }

    let duration = start.elapsed();
    // This is the "hot path" code. It's clean, safe, and ridiculously fast.
    base[0..16].to_string()
}