And what did you try to prove here? There is no use after free and no runtime error in this rust code. The socket stays valid since the moment of its creation and for the whole lifetime of the network component. It gets moved out of nested scope properly and gets closed after leaving the outer scope, after dropping the NetworkComponent struct.
The "oops" comment is invalid in your Rust example because the socket is still valid at that point.
Which is totally different than what would happen in C#, where you'd get use-after-free bug (actually use-after-close).
Try with resources is not RAII. It is a lot weaker.
The playground link confirms the socket is closed after dropping networkComponent.
Last two lines of the output:
Dropping NetworkComponent
Dropping NetworkSocket
Btw: you probably fooled yourself by accidentally creating 2 sockets, and indeed the first one gets dropped immediately when you lose (overwrite) the reference to it. Use Option to avoid that.
By replacing the socket now the port number is another one, and all processes that had open connections to that port will now crash, or have messages dropped without getting why.
I can also fabricate plenty of error situations with Rust if you feel so inclined.
No I didn't. That line is totally irrelevant and does not apply to the socket that was passed to the NetworkComponent. It applies to the initial socket you've added which was not even present in the original example. You should have used Option to make your code equivalent.
Anyway, your example failed to show use-after-close in Rust.
> Remember, Rust isn't perfect, and only fixes 70% of existing error patterns
Sure, no-one here debated that. But it fixes/protects from more error patterns than C#, and use-after-close is one of them.
You stated that try-with-resources + struct types are functionally equivalent to RAII. My code proved they were not, because you can trivially make use-after-free, and it is even really easy to do that by accident. There is nothing in the language that protects from leaking a closeable reference from the `using` scope and then using that reference after the scope gets closed. And that leak can happen 10 layers below, when it is not as easily seen as in this trivial example I posted. In Rust you can do it only with explicit `unsafe`; otherwise the typesystem tracks that for you.
Here is your Rust version, enjoy.
https://play.rust-lang.org/?version=stable&mode=debug&editio...