How can I create a function with a variable number of arguments?
问题 How can I create a function with a variable number of arguments in Rust? Like this Java code: void foo(String... args) { for (String arg : args) { System.out.println(arg); } } 回答1: In general, you can't - Rust does not support variadic functions, except when interoperating with C code that uses varargs. In this case, since all of your arguments are the same type, you can accept a slice: fn foo(args: &[&str]) { for arg in args { println!("{}", arg); } } fn main() { foo(&["hello", "world", "I",