News for hackers. Stuff that matters.
Re:Rust Language Cheat Sheet by pjmlp on Sunday April 25, 2021 @07:14AM (#26931890)
This is why while I toy with Rust, I can only see it replacing C and C++ (long term), in the context of kernels, device drivers and real time scenarios where any kind of memory allocation is a no-go (MISRA style).

For anything else, it isn't worth the productivity drop of automated memory management (regardless in what form) coupled with value types.

Re:Rust Language Cheat Sheet by flohofwoe on Sunday April 25, 2021 @07:51AM (#26932040)
If you don't allocate memory in the first place, Rust also looses a lot of appeal though, because dynamic memory management is often the root cause of memory corruption issues in C and C++ (while working with a upfront defined static memory layout avoids some typical footguns, because of things like stable memory locations throughout the lifetime of the application). I'm aware that Rust also helps to make accessing "static" memory safe, but Rust's safety guarantees are much more important with complex memory management patterns.
Re:Rust Language Cheat Sheet by yakubin on Sunday April 25, 2021 @08:20AM (#26932169)
While it is true that dynamic memory management in C and C++ is a source of memory corruption issues, the other great source is precisely the reluctance of many C and C++ programmers to use dynamic memory allocation instead of buffers of statically-defined sizes. E.g.

1. Functions which convert a number to a string, where the target string is a byte buffer passed into the function as a pointer argument. This allows C developers to pass pointers to statically-sized arrays of bytes, which leads to buffer overflows. (Inb4 someone shows up with "but that's the programmer's fault for not using an array that's big enough".) Example: strcpy.

2. Functions which return pointers to static storage, which makes them thread-unsafe, and even in single-threaded cases is error-prone, when you call one such function multiple times in succession (aliasing). Example: gmtime.

Both cases are very prominent in the standard library.

If instead functions allocated memory dynamically, then they would avoid buffer overflow, because size would be handled exclusively within those functions, allocating as much memory as is needed, no more, no less; instead of forcing the programmer to worry about the size at every call-site. They could also be easily made thread-safe and less error-prone (eliminating aliasing), provided that the allocator is thread-safe.

Another issue with the first case is that it leads to uninitialised variables. Often a pattern of "bool NumToString(int n, char* dst, int size);" is employed instead of "Optional<std::string> NumToString(int n);". This invites the possibility of using "dst" without checking the return value, which says if the conversion succeeded. In this case dst is most likely uninitialised, and definitely incorrect. An "Optional<std::string>" solves both issues.

Re:Rust Language Cheat Sheet by flohofwoe on Sunday April 25, 2021 @08:30AM (#26932223)
All true, but on the other hand you have much more dangerous (because hidden) memory corruption problems in C++ caused by hidden dynamic memory management in the C++ stdlib like iterator invalidation, or dangling std::string_views. And IMHO those cases where C++ suddenly pulls the rug from under your references are much more dangerous than working with a static memory layout in C.

The C stdlib string API is rubbish of course (along with most other C stdlib APIs). But at least it's quite simple in C to ignore the stdlib (and use better 3rd-party-libs instead) without loosing important language features.

Re:Rust Language Cheat Sheet by pjmlp on Sunday April 25, 2021 @09:08AM (#26932408)
The experience with CVEs in MISRA-C(++) vs Java and Ada embedded deployments, proves that there is still room for improvement by pushing C and C++ completely out of the picture.
Re:Rust Language Cheat Sheet by jimktrains2 on Sunday April 25, 2021 @08:05AM (#26932095)
Rust also helps prevent many types of concurrency bugs, and the type system is much more expressive than c's or c++'s, which can lead to cleaner code as well as preventing bugs like null references.
Re:Rust Language Cheat Sheet by pjmlp on Sunday April 25, 2021 @09:11AM (#26932424)
Those exposed via thread access to in memory data, for sure.

However we are slowly moving back into multi-processes as way to avoid in-process security exploits and improve overall application stability.

In these kind of scenarios, there is little that Rust type system can help regarding races in IPC.

Re:Rust Language Cheat Sheet by mxcrossb on Sunday April 25, 2021 @07:47AM (#26932027)
I’m pretty sure that’s the point. But since it’s trendy people try to apply it to everything.
Re:Rust Language Cheat Sheet by cytzol on Sunday April 25, 2021 @10:02AM (#26932757)
> But since it’s trendy people try to apply it to everything.

When people do things you don't understand, don't just say that they're just doing it because it's fashionable. Sure, they might be — or they have their own reasons.

I use Rust in a couple of places outside of its original "systems programming" niche, where, yeah, I don't really need to track every allocation, I don't care about GC pauses, I don't need to ship one single binary, and the overhead of a runtime wouldn't bother me. Things like Web servers for side projects, or small scripts to do a task I need to automate.

However, I found that:

• The effort it took to bring Rust out of its niche, and the time it took to learn the domain-specific libraries for my use cases (Rocket for Web stuff; duct for shell script stuff) was less than I thought;

• The amount of knowledge I needed to retain to use a programming language effectively — the components of its standard library, common third-party helper libraries, how to navigate the documentation, how to fix mistakes, how to avoid traps and pitfalls, how to structure your program, how to handle differences between language versions — was much larger than I thought!

So I stick with Rust for non-systems tasks because the benefits outweigh the detriments for me.

(Granted, I was only able to do this because I already knew my way around the language and the borrow checker; if you already know, say, Python, you can make this exact same argument in reverse. But then you need to know how to wield Python for low-level programming as well as high-level programming.)

I read an article a few years ago called Java for Everything[1] (which was discussed here on HN[2]) that makes the same point, only with Java. If I had to pick an "everything language", I don't think it would be 2014-era Java, but the article did sell me on the benefits of having an "everything language" in the first place, and I feel the same benefits apply here with Rust.

[1]: https://www.teamten.com/lawrence/writings/java-for-everythin... [2]: https://news.ycombinator.com/item?id=8677556

Re:Rust Language Cheat Sheet by hu3 on Sunday April 25, 2021 @08:24AM (#26932187)
Some go as far as saying that "Rust is pretty accessible to developers coming from scripting languages (JavaScript, Python, etc)." [1].

Meanwhile in the real world: "I find myself hanging for long periods of time on borrow checker errors. One of the errors has stopped my progress dead for a week now, I swear it worked a week ago and then Rust decided that a borrow I was doing was no good." [2]

[1] https://news.ycombinator.com/item?id=26929012

[2] https://news.ycombinator.com/item?id=23807574

Re:Rust Language Cheat Sheet by jhugo on Sunday April 25, 2021 @09:14PM (#26938316)
The borrow checker is deterministic, so the commenter in [2] is mistaken about it “deciding” that a previously-OK borrow was suddenly no good.

I do recall the steep learning curve and the frustrations with the borrow checker, but I haven’t had it impede me at all in probably two years now and I’m writing Rust most of the day every day.

Also, most of the knowledge I gained by learning to work with it is general knowledge that made me a better programmer, not specific knowledge of how to “work around” the borrow checker.