rustHow to join two Rust slices?
Joining two Rust slices can be done using the concat() method. This method takes two slices and returns a new slice containing all the elements of both slices.
Example
let slice1 = [1, 2, 3];
let slice2 = [4, 5, 6];
let joined_slice = [slice1, slice2].concat();
println!("{:?}", joined_slice);
Output example
[1, 2, 3, 4, 5, 6]
The concat() method takes two slices and returns a new slice containing all the elements of both slices. The elements of the first slice are followed by the elements of the second slice.
Code explanation
let slice1 = [1, 2, 3];: This line creates a slice containing the elements1,2, and3.let slice2 = [4, 5, 6];: This line creates a slice containing the elements4,5, and6.let joined_slice = [slice1, slice2].concat();: This line calls theconcat()method on the slicesslice1andslice2, and stores the result in the variablejoined_slice.println!("{:?}", joined_slice);: This line prints the contents of thejoined_slicevariable.
Helpful links
Related
- How to calculate the sum of a Rust slice?
- How to create a Rust slice with a specific size?
- How to push an element to a Rust slice?
- How to map a Rust slice?
- How to check for equality between Rust slices?
- How to convert a Rust slice of u8 to u32?
- How to reverse a Rust slice?
- Does Rust perform bounds checking on slices?
- How to convert a Rust slice of u8 to a string?
More of Rust
- Generator example in Rust
- How to replace a capture group using Rust regex?
- How to match a URL with a regex in Rust?
- How to use binary regex in Rust?
- How to use regex lookahead in Rust?
- How to make regex case insensitive in Rust?
- Regex example to match multiline string in Rust?
- How to use regex to match a double quote in Rust?
- Yield example in Rust
- How to use a tuple as a key in a Rust HashMap?
See more codes...