BorrowMut in std::borrow - Rust (original) (raw)

pub trait BorrowMut<Borrowed>: Borrow<Borrowed> where
    Borrowed: ?Sized, {
    fn borrow_mut(&mut self) -> &mut Borrowed;
}

Expand description

A trait for mutably borrowing data.

As a companion to Borrow this trait allows a type to borrow as an underlying type by providing a mutable reference. See Borrowfor more information on borrowing as another type.

Mutably borrows from an owned value.

use std::borrow::BorrowMut;

fn check<T: BorrowMut<[i32]>>(mut v: T) {
    assert_eq!(&mut [1, 2, 3], v.borrow_mut());
}

let v = vec![1, 2, 3];

check(v);

Run