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 calculate the sum of a Rust slice?
- How do I create an array of strings in Rust?
- How to replace all matches using Rust regex?
- How to use regex to match a double quote in Rust?
- Hashshet example in Rust
- How to use regex captures in Rust?
- How to convert JSON to a struct in Rust?
- How to pop an element from a Rust HashMap?
- How to convert a Rust HashMap to a JSON string?
See more codes...