expression
REST: scope quotas by authenticated principal
A quota can be scoped to a verified account or tenant rather than a global counter. This bounded synthetic data set separates two accounts. It is not an authentication implementation; real services must limit key cardinality and coordinate concurrent requests. Standalone synthetic example. Expected stdout (not executed): alice: true alice: true bob: true alice: false Common mistake: Trusting a caller-supplied account header as verified identity.
Formal Syntax & Structure
rust syntaxrust
HashMap::entry(principal).or_default()Working Example
Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { let mut counts = std::collections::HashMap::new(); for principal in ["alice", "alice", "bob", "alice"] { let count = counts.entry(principal).or_insert(0_u32); let allowed = *count < 2; if allowed { *count += 1; } println!("{principal}: {allowed}"); }}