How to create a function with variable number of arguments in Rust?
Hello everyone! I just started learning Rust after C++ and Python, and I ran into a problem. In C++ I can write a function with variable number of arguments using `...` (variadic templates), and in Python — using `*args`. For example, I want to create a `sum_all` function that adds any number of integers:
```rust
// How do I do this in Rust?
fn sum_all(...) -> i32 {
// something here
}
fn main() {
println!("{}", sum_all(1, 2, 3)); // 6
println!("{}", sum_all(10, 20, 30, 40)); // 100
}
```
I've read that Rust doesn't support classic variadic functions like in C. But there are macros? Or can I use slices? I tried passing a vector, but it's inconvenient — I have to create `vec!` every time. Is there an idiomatic way to do this in Rust? I need exactly a re
No answers yet.
Log in to leave an answer
Blogs
Optimizing JavaScript Application Performance on Node.js