Rust Examples

Page 2 of 8

Browse Rust examples

rustSnippet

Expand Compact Range Notation

Expand compact range notation like 1-3,5,8-9 by splitting on commas, matching each part with split_once, and flat_mapping ranges into one vector.

rustSnippet

Fetch Update Folds The CAS Retry

fetch_update folds the compare-and-swap retry loop into one call: Ok carries the previous value when the closure wrote, Err carries it when the closure refused.

rustSnippet

Sync All Before Rename

Stage a write with write_all, force it to disk with sync_all, then publish with fs::rename so a crash never leaves a half-written live file.

rustSnippet

Next Permutation In Place

Find the rightmost ascent, swap with the last larger element, reverse the suffix, and reverse everything when the sequence is already maximal.

rustSnippet

Command Reads Piped Stdin

Spawn a child with Stdio::piped stdin, write one buffer into its handle, then collect stdout after the write end drops and the child sees EOF.

rustSnippet

Repeated Characters Via Entry

Count characters through a HashMap entry increment, keep counts above one, and sort the pairs so reports of repeated characters are complete and stable.

rustSnippet

Reverse Words In A Sentence

Split a borrowed sentence into whitespace words, reverse the iterator, collect, and rejoin while the source string stays usable because nothing moved.

rustSnippet

Vec Swap Reorders By Index

Reorder elements in place with Vec::swap by index, then run selection-sort's opening move by finding the minimum with min_by and swapping it to front.