This crate provides vararg
proc-macro, that can be applied to any
function which last arg is Vec
, array or reference to slice, turning
it into variadic macro, similar to println!
or format!
```rust use vararg::vararg;
fn vararg_func
fn main() { let s1 = "\n\ first: first \n\ last: \n\ "; let s2 = "\n\ first: first \n\ last: last \n\ "; let s3 = "\n\ first: first \n\ last: last1,last2 \n\ "; asserteq!(s1, varargfunc!("first")); asserteq!(s2, varargfunc!("first", "last")); asserteq!(s3, varargfunc!("first", "last1", "last2")); } ```
You can create vararg function without first required argument (unlike vararg functions in C or nightly Rust). For example, let's create vararg function to join String slices without separator:
```rust use vararg::vararg;
fn join_strs
fn main() { let s1 = ""; let s2 = "ha"; let s3 = "hahaha"; asserteq!(s1, joinstrs!()); asserteq!(s2, joinstrs!("ha")); asserteq!(s3, joinstrs!("ha", "ha", "ha")); } ```
You also can change name of generated macro or type of last arg
```rust use vararg::vararg; use std::process::Command;
fn execprocess(basename: &str, args: &[&str]) -> String { String::fromutf8( Command::new(basename) .args(args) .output() .expect("failed to execute echo") .stdout, ) .expect("failed to parse hello") }
fn main() { let (name, c) = if cfg!(targetos = "windows") { ("cmd", "/C") } else { ("sh", "-c") }; asserteq!("hello\n", exec!(name, c, "echo hello")); } ```
Licensed under either of Apache License, Version 2.0 or MIT license at your option.