by brighteyes · 7y ago · 0 comments
> C#, a programming language developed by Microsoft, also features some memory access improvements but is not as advanced as Rust. [..] Besides [Rust] being superior to C# in regards to better memory protections
That's false, isn't it? C# is a memory safe language, period. It relies on GC for that.
In fact C# has arguably better memory safety than Rust because you can do things safely in C# that you would be forced to use 'unsafe' in Rust for. (Granted, then you have overhead from GC, but that's not what the author is talking about.)
> Rust is also more popular with developers these days and might be easier to recruit for.
The author has misinterpreted what "most loved" means in the quoted survey: it means that among Rust developers, it gets a very high rating. That says nothing about how big that group is, nor how popular it is in the general population of developers.
The Rust community is growing but still very small - it's an emerging language. Almost everyone that uses it decided to use it because they like it. (That doesn't diminish the accomplishment - there are other emerging languages that are not as loved by their users.)
The MSRC article says:
>in a new blog series we will highlight Microsoft’s exploration of safer system programming languages, starting with Rust
so it appears the zdnet author misinterpreted this to mean it hasn't started using it yet.
As an example, https://github.com/Azure/iotedge had its first Rust code checked in in February 2018.
MS has also already been contributing to Rust and LLVM to get it working on Windows ARM32 and ARM64.
There are several azure services running at least some Rust and job descriptions for IOT stuff have mentioned it for months.
It's quite possible that what the author intends by “memory protections” is broader than what is captured by “memory safety”; e.g., the borrower checker is, in a sense, a way of protecting memory from unexpected changes in parallel tasks.
It is definitely false for the author to say Rust is "superior to C# in regards to better memory protections", in any definition of "memory protections".
Or, did someone prod a journalist with little familiarity in programming theory, to write positive "spin" on Rust being explored by Microsoft? Is it even news for the world's largest software company (by market cap), to explore the use of emerging languages?
I missed the part where whether the author used words and, if so, which words they were was a matter of controversy.
That's the part you cited, and went on to imply the author might be referring to Rust's borrower checker. Nothing from the author's "wording" implies knowledge of Rust's memory protection techniques
The original post's whole sentence there is basically nonsense. They're different enough that it's hard to call either one more advanced or having better memory protection at all.
Using just plain C#, it has better memory protection, since the GC and runtime make it impossible to leak, double-free, or access out-of-bounds. But there's a higher cost for that, and the system for managing non-memory resources is not as reliable - you can leak file handles, network handles, etc more easily. Not up on the latest in Rust, but I think it's possible to leak memory if you are sufficiently clever and doing really weird stuff. But the standard ownership model is great at making it really hard to leak or mismanage any resource, not just memory.
It's hard to call either one more advanced either. .NET has a massive std lib and C# has been getting some cool new features. It's still a runtime language though, with the limitations that come from that. Rust's lifetime and ownership system is pretty advanced, but wouldn't make sense for a runtime language.
This is false.
Neither C# nor Rust protect from memory leaks. There are actually some gotchas in C# that can cause memory leaks - for example, you have to be very careful about events. Memory leaks are not memory unsafe, though.
On the topic of actual memory safety - C# has unsafe blocks just like Rust does. Safe Rust is just as safe from memory safety problems as safe C#, even more so, because C# doesn't require unsafe for FFI, where all bets are off. And unsafe C# is just as unsafe as unsafe Rust can be.
In Rust you can leak due to reference cycles for example: https://doc.rust-lang.org/book/ch15-06-reference-cycles.html
C# will clean up reference cycles, but an unintended reference can cause leaks (if you forget to remove it).
In both languages such leaks are not good, but at least they aren't memory safety issues.
The only gotcha that can cause memory leaks in C# is a GC root, i.e. referencing something on a stack, that is static or that is pinned. Events/delegates are not special in any way, they are simply a reference footgun because it's non-obvious that they contain a reference to the object that contains the handler.
Note that Rust provides an easy way to intentionally leak memory, unlike many other languages: https://doc.rust-lang.org/std/mem/fn.forget.html
`Box::leak()` leaks memory.
True. I think they are conflating memory-safety and thread-safety here. Rust definitely offers better thread-safety properties. This is somewhat nice, since in my experience thread-safety issues are the more commonly undetected bugs - which are later on tricky to find and might require big refactoring efforts to fix. They are also often the source of the memory-unsafety issues.
Imho Go currently has the best blend of both properties for a GCed language - since the race detector does a good job of detecting thread-safety issues. I wish for Java and C# to get the same.
See this example where I break encapsulation in a struct by abusing a race condition:
https://github.com/VictorGavrish/MultiThreading/blob/master/...
Ah, that is also false. A GC makes a language safer, but it does not protect against race conditions, so it is not entirely memory safe.
Of course, many languages that are referred to as memory safe have this loophole, but that may also be due to there not previously being a concurrent language with suitable protection in this area.
It's definitely true that this does not imply thread-safe, and the ability to prevent the mess up the consistency of valid objects.
This is absolutely false. Race conditions in native structures can very easily invalidate any memory safety guarantees provided by the language, causing issues such as buffer overflows.
In other words, the presence of any memory unsafe operation leads to a total and immediate loss of all memory safety.
See for example how races in Go can lead to type confusion for interface types (fat pointers) through store tearing (non-atomic writes): https://research.swtch.com/gorace or https://groups.google.com/forum/#!msg/golang-dev/mXjFejQ5A3Q...
The former has a detailed explanation, the latter has a code example that shows more clearly what the technique can be used to do (cast a struct A to unrelated struct B with different length, thus allowing out-of-bounds reads/writes).
If on the other hand you were just saying that these languages are just called memory safe, even though they aren't, then yes. That's what I said.
Could you provide an example? I thought pretty much everything could be achieved, albeit sub-optimally with reference counting (data structures).
https://msdn.microsoft.com/en-us/magazine/mt814808.aspx
https://docs.microsoft.com/en-us/dotnet/api/system.span-1?vi...