Search

Lib.rs

› Rust patterns
#mutable-reference #split #reference

splitmut

Safely retrieves multiple mutable values from the same collection

by David Henningsson and 2 contributors

  • Install
  • API reference
  • GitHub (diwic)

2 unstable releases

Uses old Rust 2015

0.2.1 Feb 27, 2018
0.1.1 Feb 5, 2016
0.1.0 Feb 4, 2016

#2559 in Rust patterns

Download history 1044/week @ 2025-12-31 2798/week @ 2026-01-07 2593/week @ 2026-01-14 1933/week @ 2026-01-21 2466/week @ 2026-01-28 2812/week @ 2026-02-04 2874/week @ 2026-02-11 3184/week @ 2026-02-18 2504/week @ 2026-02-25 4836/week @ 2026-03-04 4733/week @ 2026-03-11 3466/week @ 2026-03-18 2589/week @ 2026-03-25 3321/week @ 2026-04-01 3337/week @ 2026-04-08 3710/week @ 2026-04-15

13,692 downloads per month
Used in 30 crates (via franklin-crypto)

Apache-2.0/MIT

17KB
199 lines

SplitMut

A Rust library for safely retrieving multiple mutable values within the same collection.

API Documentation Crates.io

get2_mut, get3_mut and get4_mut return a tuple or 2, 3 or 4 values, each one of them being one of:

  • Ok(&mut V)
  • Err(SplitMutError::NoValue) in case there was no value for the key (i e, when your usual get_mut would have returned None)
  • Err(SplitMutError::SameValue) in case the same value has already been returned earlier in the tuple.

Add use splitmut::SplitMut to your code have these functions implemented for mutable slices, Vec, VecDeque, HashMap and BTreeMap.

Example

extern crate splitmut;

use std::collections::HashMap;
use splitmut::{SplitMut, SplitMutError};

// Create a hashmap
let mut h = HashMap::new();
h.insert(1, "Hello");
h.insert(2, "world");

// Swap two values easily
{
    let (m1, m2) = h.get2_mut(&1, &2);
    std::mem::swap(m1.unwrap(), m2.unwrap());
}
assert_eq!(h.get(&1), Some(&"world"));
assert_eq!(h.get(&2), Some(&"Hello"));

// Show error handling
let (m0, m1a, m1b) = h.get3_mut(&0, &1, &1);
// No value for the key "0"
assert_eq!(m0, Err(SplitMutError::NoValue));
// First value for the key "1" is returned successfully
assert_eq!(m1a, Ok(&mut "world"));
// Second value for the key "1" returns an error
assert_eq!(m1b, Err(SplitMutError::SameValue));

No runtime deps

Related: inner, dbus-tree, dbus-codegen, dbus-crossroads, alsa-sys, dbus-tokio, libdbus-sys, alsa, dbus
See also: scoped-tls-hkt, shell-words, text-splitter, shlex, gix-index, orx-split-vec, async-dup, maplit, workflow-graph-scheduler, orx-fixed-vec, collection_literals

Lib.rs is an unofficial list of Rust/Cargo crates, created by kornelski. It contains data from multiple sources, including heuristics, and manually curated data. Content of this page is not necessarily endorsed by the authors of the crate. This site is not affiliated with nor endorsed by the Rust Project. If something is missing or incorrect, please file a bug.