rustHow to join two arrays in Rust
Joining two arrays in Rust can be done using the concat()
method. This method takes two arrays as arguments and returns a new array containing all the elements of both the arrays.
Code example:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let array3 = array1.concat(array2);
println!("{:?}", array3);
Output [1, 2, 3, 4, 5, 6]
Explanation:
- The
let
keyword is used to declare a variable. Here, two variablesarray1
andarray2
are declared and assigned with two arrays containing elements1, 2, 3
and4, 5, 6
respectively. - The
concat()
method is used to join two arrays. It takes two arrays as arguments and returns a new array containing all the elements of both the arrays. - The
println!
macro is used to print the elements of the arrayarray3
which contains the elements of both the arraysarray1
andarray2
. - The
{:?}
is used to print the elements of the array in a debug format.
Helpful links:
More of Rust
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to parse a file with Rust regex?
- How to use regex to match a double quote in Rust?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- How to perform matrix operations in Rust?
- How to push an element to a Rust slice?
- How to convert a Rust slice of u8 to a string?
- How to extract data with regex in Rust?
See more codes...