expression

Limit simultaneous operations with Semaphore

A permit represents one share of a limited resource. Holding it across an operation reserves that share; dropping the permit returns it. This bounds concurrent occupancy, not requests per second. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 0 1 Common mistake: Confusing a concurrent-work limit with a time-based rate limit.

Formal Syntax & Structure

rust syntaxrust
sem.acquire().await -> Result<SemaphorePermit, AcquireError>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let sem = tokio::sync::Semaphore::new(2);    let a = sem.acquire().await.unwrap();    let b = sem.acquire().await.unwrap();    println!("{}", sem.available_permits());    drop(a);    println!("{}", sem.available_permits());    drop(b);}
Rust Reference