4 ms·
The ? is not really the issue here. Rust is similar to C++ in that it encourages implicit resource management using scopes, so if you have resources that have t
by filmor 2y ago
The ? is not really the issue here. Rust is similar to C++ in that it encourages implicit resource management using scopes, so if you have resources that have to be free'd, you have to implement Drop somewhere.
The code would have looked just as correct and would have been just as wrong with the "old" non-sigil `try!(...)` syntax.
- akira2501 2y ago> The ? is not really the issue here. Based upon the way the code was written, it was, at least in the mind of the author. They forgot they could exit scope there. They clearly didn't _intend_ for that outcome, but ended up with it anyways, possibly out of habit, and possibly because a single impactful sigil like that is easy to miss in review. I get that technically it didn't cause the memory leak.. but just look at the way that was written initially... it obviously led to it _within_ that particular structure.
- nemetroid 2y ago> They forgot they could exit scope there. Their mistake was writing resource management code that requires manually thinking about where they might exit scope.
- oguz-ismail 2y agoJust curious, would writing it in a better language have prevented this?
- Dx5IQ 2y agoThis is a classic problem you can make in any language. If you were doing Java, and forgot to use `finally` for a resource, you'd have a leak due to an exception. I do not know what metric you use to define "better"
- oguz-ismail 2y ago`finally' is a word though, harder to forget than a single punctuation mark.
- filmor 2y agoThe dev did not "forget a single punctuation mark". It was there and it was correct. What was /not/ correct is that he did not implement any form of implicit resource freeing in an RAII-language. Doing resource management using scopes is a choice. It's not inherently better or worse than explicit resource management with something like Go's `defer`. But if that choice is made, as in Rust or C++, you have to ensure that the implicit resource management is correctly implemented (Drop in Rust, destructors in C++). Same as in Go, where you have to ensure that you call `defer` with the correct cleanup function or in C# where you have to use `using` on your disposables, etc.
- nemetroid 2y agoSlightly tongue in cheek answer: it is easy to write this kind of code in C++ without having C involved, hence style guides will usually have a prominent guideline specifically against this type of code[1]. In Rust, I think you only really run into this issue when interacting with C (or otherwise engaging in unsafe code), so for normal Rust coding it doesn't need to be spelled out as a guideline. And the Rustonomicon[2], the go-to resource for unsafe Rust, isn't really written as a set of guidelines. At least from a brief search, I found it harder to find a Rust page that specifically says "don't do this". 1: E.g. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-raii https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines..., the first guideline in the "resource management" section. 2: https://doc.rust-lang.org/nomicon/intro.html https://doc.rust-lang.org/nomicon/intro.html