rustHow to borrow from vector in Rust
Rust provides a convenient way to borrow from a vector using the slice
method. This method takes two arguments, the start index and the end index of the slice. The following example code will borrow the elements from index 1 to index 3 from a vector:
let v = vec![1, 2, 3, 4, 5];
let s = &v[1..3];
The output of the above code will be a slice containing the elements [2, 3]
:
[2, 3]
The ## Code explanation
let v = vec![1, 2, 3, 4, 5];
: This line creates a vectorv
containing the elements[1, 2, 3, 4, 5]
.let s = &v[1..3];
: This line creates a slices
containing the elements from index 1 to index 3 from the vectorv
.
Helpful links
Related
More of Rust
- How to use regex to match a group in Rust?
- How to use regex to match a double quote in Rust?
- How to use regex with bytes in Rust?
- How to parse JSON string in Rust?
- How to get a capture group using Rust regex?
- Word boundary example in regex in Rust
- How to implement PartialEq for a Rust HashMap?
- How to convert a Rust HashMap to JSON?
- How to calculate the inverse of a matrix in Rust?
- How to replace a capture group using Rust regex?
See more codes...