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
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to get the last element of a Rust slice?
- How to escape dots with regex in Rust?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex with bytes in Rust?
- Yield example in Rust
- How to split a string with Rust regex?
See more codes...