3 ms·
> I want two arguments of any type I can transform to an owned string Since Rust 1.26 you can write this: fn foo(x: impl Into<String>, y: impl Into<String
by oconnor663 4y ago
> I want two arguments of any type I can transform to an owned string
Since Rust 1.26 you can write this:
fn foo(x: impl Into<String>, y: impl Into<String>) {
...
}
Which is almost exactly the same as this:
fn foo<T: Into<String>, U: Into<String>(x: T, y: U) {
...
}
- zozbot234 4y agoBut this has the same flaw as the original code in OP, where you need a separate inner function to avoid excess monomorphization. What's wrong with just taking a String (or a ref to Path) and expecting user to add .into() at the calling site?
- scottlamb 4y agoBasically you have the choice of adding a tiny bit of boilerplate in the caller, having monomorphization bloat, or adding lots of boilerplate to the callee. In general I'm with you: just stick the .into() in the caller. But this is pub std code, expected to be called from many many places, so they probably made the right choice to make it more convenient to call.
- estebank 4y agoI do want to highlight a comment in passing in the OP: the team has plans (and some work) around accomplishing that optimization in an automatic way, without extra syntax, under the whimsical name of polymorphisation.
- ssokolow 4y agoIf it really matters, you could always use the #[momo] procedural macro by Andre "llogiq" Bogus, one of the big names in the Rust community: https://lib.rs/crates/momo https://lib.rs/crates/momo