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
letkeyword is used to declare a variable. Here, two variablesarray1andarray2are declared and assigned with two arrays containing elements1, 2, 3and4, 5, 6respectively. - 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 arrayarray3which contains the elements of both the arraysarray1andarray2. - The
{:?}is used to print the elements of the array in a debug format.
Helpful links:
More of Rust
- Regex example to match multiline string in Rust?
- How to map a Rust slice?
- How to replace a capture group using Rust regex?
- How to use regex captures in Rust?
- How to create a HashMap of structs in Rust?
- How to use non-capturing groups in Rust regex?
- 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 use regex lookbehind in Rust?
See more codes...