rustExample of borrow_mut in Rust
Borrow_mut
is a Rust language feature that allows a mutable reference to be taken from a data structure. This allows the data structure to be modified without taking ownership of it.
Example code
let mut x = vec![1, 2, 3];
let y = &mut x;
y.push(4);
Output example
[1, 2, 3, 4]
Code explanation
let mut x = vec![1, 2, 3];
: This line creates a mutable vectorx
with the values1
,2
, and3
.let y = &mut x;
: This line creates a mutable referencey
to the vectorx
.y.push(4);
: This line adds the value4
to the vectorx
using the mutable referencey
.
Helpful links
Related
- How to borrow with lifetime in Rust
- How to borrow a string in Rust
- How to borrow as static in Rust
- When to use borrow in Rust
- How to borrow moved value in Rust
- How to return borrow in Rust
- Rust partial borrow example
- How to borrow in loop in Rust
- How to borrow hashmap in Rust
- How to borrow from vector in Rust
More of Rust
- How to use regex to match a double quote in Rust?
- How to use regex lookahead in Rust?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
- How to use backslash in regex in Rust?
- How to parse JSON string in Rust?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to make regex case insensitive in Rust?
- How to convert a Rust HashMap to a BTreeMap?
See more codes...