How to properly concatenate vectors in Rust: best ways to merge
Hello everyone! I've just started learning Rust and encountered a problem when working with vectors. I need to merge two vectors into one, but I'm not sure which way is correct and efficient.\n\nLet's say I have two vectors:\n```rust\nlet vec1 = vec![1, 2, 3];\nlet vec2 = vec![4, 5, 6];\n```\nI want to get `[1, 2, 3, 4, 5, 6]`. I tried writing `let result = vec1 + vec2;`, but the compiler complains. Then I came across the `append` method, but it mutates the original vector and takes data from the second one. I also saw `extend` and `chain`, but got confused about their differences.\n\nPlease advise:\n1. Which way is the most idiomatic for Rust?\n2. Which is the most performant for large vectors?\n3. What to do if I need to keep the original vectors unchanged?\n\nThanks in advance for your help!
<<>>
No answers yet.
Log in to leave an answer