Skip to main content

with_rng

Function with_rng 

Source
pub fn with_rng<F, R>(f: F) -> R
where F: FnOnce() -> R,
Expand description

Scope guard for RNG operations.

Executes a closure with RNG state properly managed. This is a convenience wrapper around RngGuard.

§Example

use miniextendr_api::rng::with_rng;
use miniextendr_api::ffi::unif_rand;

let values = with_rng(|| {
    (0..10).map(|_| unsafe { unif_rand() }).collect::<Vec<_>>()
});

§Warning

Like RngGuard, this relies on Rust drop semantics and won’t properly clean up if R longjumps. For R-exposed functions, use #[miniextendr(rng)] instead.