Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 957 Bytes

File metadata and controls

40 lines (30 loc) · 957 Bytes
minutes 2

[method]_mut: Mutable reference access

Suffix for access-style methods.

# // Copyright 2025 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
impl<T> Vec<T> {
    fn get(&self, index: usize) -> Option<&T>;
    fn get_mut(&mut self, index: usize) -> Option<&mut T>;
}

impl<T> [T] {
    fn iter(&self) -> impl Iterator<Item = &T>;
    fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>;
}
Details
  • Suffix that signifies the method gives access to a mutable reference.

  • Requires mutable access to the value you're calling this method on.

  • Rust can't abstract over mutability, so there's no way to write a method that can be used both mutably and immutably. Instead, we write pairs of functions, where the immutable version gets the shorter name and the mutable version gets the _mut suffix.