4 ms·
> As an aside, the Rust code could also be written like this Sadly it can't really, because Rust doesn't have control-flow based type refinement like TypeScrip
by phiresky 6y ago
> As an aside, the Rust code could also be written like this
Sadly it can't really, because Rust doesn't have control-flow based type refinement like TypeScript.. In TS the type of `file` after the throw is File. In Rust it stays `Option<file>` so you would have to unsafely unwrap it below the if block.
- mikepurvis 6y agoRust’s if-let construct handles that in a nice, explicit, and compact way.
- steveklabnik 6y ago(Unwrap is not unsafe, and in fact, in this code, you would even know that it can never panic. That being said, you're not wrong that this is nicer in TS. You could invert the condition and add an else and it would be not too terrible. I'd still probably go for a ? style.)