Multiple type variadic args

Seems like Rust has no native support for variadic args. However, we can achieve the same with macro_rules. But, that approach is restricted to a single type for each of the var-arg.

Is there a way to have multi type variadic args? For example consider a simple function which executes a parameterised SQL query by accepting a query and list of params to be passed. The size of the list depends on the query therefore we cannot use tuple. Here we are restricted to only params of type T (say u32). What if the query involves a param sequence like (u32, str, bool)?

pub fn execute_sql_query<T>(query: &str, params: &[&T]) {
    let conn = get_connection();
    conn.query(query, params)
}

That's really a language design issue, so is addressed in the Rust internals forum (IRLO), which is the appropriate place for discussing language and tool enhancements/changes. There's an ongoing discussion there.

2 Likes

As an aside, for the specific case of SQL query parameter binding, you can pass an array/slice of trait objects in the meantime. For example, the SQLite3 bindings solve it this way.

1 Like

Thanks! I was not aware about the language design not covering the same and that discussion around this had started recently.