Rust Examples

Browse Rust examples

rustSnippet

Vec leak Into Static Slice

Convert an owned Vec into a static-lifetime slice with Vec::leak, trading the deallocation path for data that lives for the whole program.

rustSnippet

Checked Arithmetic On Durations

Enforce duration budgets with checked_add and checked_sub, which return Option instead of wrapping past zero the way Duration cannot go negative.

rustSnippet

Spawn And Poll With try_wait

Start a child process with spawn and probe it non-blockingly via try_wait: None means still running, and wait() finally blocks for the exit status.

rustSnippet

Fences Order Flag And Payload

A Release fence before publishing a flag pairs with an Acquire fence after observing it, so payload writes are visible without atomic ordering on each access.

rustSnippet

Clone Ends The Borrow Early

Turn a map lookup into an owned value with cloned so the borrow ends immediately, letting later inserts into the same HashMap borrow-check cleanly.

rustSnippet

Copy Files With Std Fs

Copy a file with fs::copy inside a fallible main, assert the byte count, and compare content line by line before cleaning up both temporary files.