4 ms·
IIRC in Rust it has to be either of the Ok or Err variants and the enclosed type can then provide its own "zero value" via the standard library "Default" [0] tr
by Lev1a 5y ago
IIRC in Rust it has to be either of the Ok or Err variants and the enclosed type can then provide its own "zero value" via the standard library "Default" [0] trait or your own implementation for the particular type.
That trait is also implemented not for "Result" but for "Option" giving it a default value of "None".
[0]: https://doc.rust-lang.org/std/default/trait.Default.html https://doc.rust-lang.org/std/default/trait.Default.html
- sacado2 5y agoYes, for Option the default value is quite obvious. I'm talking about Result, because it's the second best-known sum type, and it doesn't have an obvious default value.
- deleted 5y ago[deleted]
- Lev1a 5y agoBindings in Rust have to initialized with an explicit value if the first use comes before the first modification/assignment otherwise [0] you get an error message with error E0381 [1]. Even if that explicit assignment is just through the use of the type's default method [2], if available. Since it's implemented for Option<T> you can do it the same way [3] for that, but that's not available for Result<T,U> since the semantics are different from Option which is basically just Rust's alternative for 'null'/'nil' (but verifiable by the compiler). Thus you'd have to choose what variant of the enum you want to have at initialization, after which you can use the Default implementation of the enclosed type, if available (and so on, if there are more nested types). Though honestly I can't recall using the kind of pattern in Rust that would "need" something like that. Through the handling of code blocks as expressions, early returns etc. you for example don't need to initialize a variable before having an if-/match-/for-/while-/etc. block, you can simply use that block as an expression on the right side of the assignment [4]. [0]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=50e8cbc37459e52eadad4384261a479e https://play.rust-lang.org/?version=stable&mode=debug&editio... [1]: https://doc.rust-lang.org/stable/error-index.html#E0381 https://doc.rust-lang.org/stable/error-index.html#E0381 [2]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=938c7a8674d72f87f0817eaa31c88f93 https://play.rust-lang.org/?version=stable&mode=debug&editio... [3]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=062ec5c467bba12fc76fa08a404a7adc https://play.rust-lang.org/?version=stable&mode=debug&editio... [4]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fab6733af1a9cd98dfd8afff1ea5c7df https://play.rust-lang.org/?version=stable&mode=debug&editio...
- foldr 5y agoIn Go, every type has a sensible built-in zero value. In Rust there is no need to worry about what the 'default' value of a Result type should be. So choosing a sensible default value for result would be a problem in Go (but not in Rust).