https://chatgpt.com/share/70af56e3-2fde-4dab-8b59-d05960dadd...
Just asking if there's a problem and asking Chat GPT to rank the error leads the memory leak error to be categorized as low criticality though.
https://chatgpt.com/share/68eb706a-05eb-44ca-8e2b-abc97c0422...
Pretty promising though that it was able to point out the problem even without pointing it out and it definitely caught the memory leak issue.
Claude 3.5 Sonnet was able to figure it out right away without pointing it out though.
Rust: drop
C++: T::~T()
C#: using + dispose
Java: try-with + autocloseable, or finally
Go: defer
Haskell: bracket
JavaScript: finally
Python: finally or with + __exit__
Don't like it? Vote for linear types.I wonder if it would be possible in Rust to do some magic at the Rust/C border in order to trick old C code into living inside an arena that is invisible to it?
As I get older my eyes strain more and all of this power packed into a single character really just puts me off. It seems like a strategy to emphasize writing code quickly rather than correctly, which is odd, given that this is opposite to the value proposition these languages purport to bring.
The code would have looked just as correct and would have been just as wrong with the "old" non-sigil `try!(...)` syntax.
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.
Their mistake was writing resource management code that requires manually thinking about where they might exit scope.
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..., the first guideline in the "resource management" section.
I do not know what metric you use to define "better"
It seems to derive from the same reason why breaks and goto are generally frowned upon if not used judiciously - they lead to unexpected control flow.
I can understand it not being intuitive to someone who has never seen it before, but that's purely a familiarity thing.
Still, lots of incorrect code has been written with that pattern as well due to incomplete cleanup, so it's not really fair to blame this syntax for it XD.
(I think most mechanisms that can jump far, including returns, need to be used carefully - nesting a return deep into a bunch of loops is just as bad)
On the contrary, IDE should fold 'if err != nil' in golang code to make meaningful code more visible.