3 ms·
I was super excited about the idea of Rust but constantly frustrated trying to use it to write real code. I've basically decided to stay in my (ever-improving)
by TillE 6y ago
I was super excited about the idea of Rust but constantly frustrated trying to use it to write real code.
I've basically decided to stay in my (ever-improving) comfort zone of C++ until Rust gets named/default arguments, which is my arbitrary litmus test for whether it's going to actually be a usable language for me.
- svnpenn 6y agoYou can do that now. Its not quite the same but pretty close: fn f(n: Option<u8>) -> u8 { match n { Some(n0) => n0, None => 10 } } fn main() { let n1 = f(Some(20)); let n2 = f(None); println!("{} {}", n1, n2); }
- djur 6y agoThis doesn't demonstrate either named or default arguments. You can expand on it to implement the equivalent of default arguments, sure, but it doesn't solve the ergonomic reason to have default arguments, and it doesn't touch on named arguments at all.
- identity0 6y agoYou're missing the point. Here's a good article that talks about the problems of having neither overloading nor default parameters, but in Go.[1] The problem is made significantly worse in Rust when all struct fields need to be initialized and there are no varargs. I really see no solution to the "stable API with more options" problem in Rust, aside from a builder pattern, which I really hate. 1: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis https://dave.cheney.net/2014/10/17/functional-options-for-fr...
- svnpenn 6y agoIn Go, you can use an interface to implement private struct fields. Then you can make a "New" function to implement default values as needed. I made a post about this last month: https://stackoverflow.com/a/63552596 https://stackoverflow.com/a/63552596
- estebank 6y agoWhy do you have the builder pattern (other than the verbosity of declaring them)? I normally just leverage a couple of features: Derive(default) to have an "empty" initialized value and the struct field filling syntax let x = Foo { field_i_care_about, ..Default::default() };
- lalaithion 6y agoNot exactly what you want, but one option is to do something like: struct OptionsStruct {...} impl Default for OptionsStruct {...} impl Add for OptionsStruct {...} fn port(portnum : u16) -> OptionsStruct {...} fn timeout(millis: u64) -> OptionsStruct {...} fn maxconns(conns: usize) -> OptionsStruct {...} fn main() { ... // startServer("localhost", default()) startServer("localhost", port(54321) + timeout(1000)); ... } Not the most ergonomic, I know, and requires custom architecting for each instance of this, but somewhat doable.