this post was submitted on 24 Nov 2024
78 points (97.6% liked)

Ask Lemmy

27036 readers
933 users here now

A Fediverse community for open-ended, thought provoking questions

Please don't post about US Politics. If you need to do this, try !politicaldiscussion@lemmy.world


Rules: (interactive)


1) Be nice and; have funDoxxing, trolling, sealioning, racism, and toxicity are not welcomed in AskLemmy. Remember what your mother said: if you can't say something nice, don't say anything at all. In addition, the site-wide Lemmy.world terms of service also apply here. Please familiarize yourself with them


2) All posts must end with a '?'This is sort of like Jeopardy. Please phrase all post titles in the form of a proper question ending with ?


3) No spamPlease do not flood the community with nonsense. Actual suspected spammers will be banned on site. No astroturfing.


4) NSFW is okay, within reasonJust remember to tag posts with either a content warning or a [NSFW] tag. Overtly sexual posts are not allowed, please direct them to either !asklemmyafterdark@lemmy.world or !asklemmynsfw@lemmynsfw.com. NSFW comments should be restricted to posts tagged [NSFW].


5) This is not a support community.
It is not a place for 'how do I?', type questions. If you have any questions regarding the site itself or would like to report a community, please direct them to Lemmy.world Support or email info@lemmy.world. For other questions check our partnered communities list, or use the search function.


Reminder: The terms of service apply here too.

Partnered Communities:

Tech Support

No Stupid Questions

You Should Know

Reddit

Jokes

Ask Ouija


Logo design credit goes to: tubbadu


founded 1 year ago
MODERATORS
 

Title. I'm looking for a concrete answer for this.

top 19 comments
sorted by: hot top controversial new old
[–] 1984@lemmy.today 24 points 2 days ago* (last edited 2 days ago) (3 children)

None of the languages give the same runtime guarantees as Rust without having a garbage collector.

I think people in this thread are putting Rust in the same bucket as garbage-collecting languages, but there is a performance cost to garbage collecting. Rust doesn't have a garbage collector and this is why Rust is very fast and still can guarantee a lot of runtime errors won't happen (unlike in C, c++ etc).

But it's really complicated to write code in Rust. Not the basic code but if you have lifetimes on things or use async code and want to change it, you may have to spend hours reworking your entire program.

[–] GBU_28@lemm.ee 14 points 2 days ago (1 children)

Agree. I get very grumpy refactoring async rust. It's the only time at the point in my career that I think "what the fuck I don't know anything about anything I think I'm a lizard"

[–] sbv@sh.itjust.works 10 points 2 days ago

Async Rust has a long way to go. Eventually someone will figure out a decent abstraction. That'll be good.

[–] leisesprecher@feddit.org 4 points 2 days ago

It would be really great to have some "Python layer" on top of Rust.

My current work is mostly data mangling web services (Java/Spring Boot) and there's simply no way I could convince anyone (including myself) that Rust is a viable alternative in terms of development speed.

[–] akkajdh999@programming.dev 6 points 2 days ago

None of the languages give the same runtime guarantees as Rust without having a garbage collector.

If you only count mature languages. There's a lot of small immature languages. For example Inko.

[–] masterspace@lemmy.ca 30 points 2 days ago (1 children)

Pretty much all high level languages do because they're already designed around automatic memory management.

[–] Dhs92@programming.dev 13 points 2 days ago (1 children)

Yeah, Rust is a special case because it handles almost everything at compile time. It also doesn't rely on garbage collecting like the majority of modern high level languages.

[–] Repelle@lemmy.world 1 points 1 day ago* (last edited 1 day ago)

Swift also handles everything at compile time, using automatic reference counting. It is also in general faster then c++ in the benchmarking I’ve done for work where we are considering incrementally replacing c++ with it. (Benchmarking was focused on very language-standard code, NOT hand-optimized code focusing on getting every last ounce of speed out).

[–] Lojcs@lemm.ee 27 points 2 days ago (1 children)

Almost every language does, do they not? Rust is special because it is safe and as fast as cpp

[–] UnfortunateShort@lemmy.world 2 points 2 days ago (1 children)

Came her to say this. Safe programming languages? Yes, plenty. Safe and low-level? Well, modern C++, kinda, if you do your homework and follow some rules. I don't know any other tbh.

[–] xigoi@lemmy.sdf.org 2 points 2 days ago

Nim? D? Ada?

[–] solrize@lemmy.world 14 points 2 days ago* (last edited 1 day ago)

If you have to ask this question you should probably be using a garbage collected language. Manual memory management is quite tedious and it's easy to make mistakes. Rust's novel contribution is reliably catching the mistakes at compile time so once you have fixed all the compile time error messages you have a safe program. But it doesn't ease the tedium that much.

GC does it automatically and is way more convenient, but inflicts a cost in runtime performance. That's almost always fine on today's computers, thus Python's popularity. Rust is best for systems work where you need more precise control of machine resources. It is probably used more than necessary right now, because it's new and exciting and programmers like that.

The safest language is probably Ada but it is less flexible than Rust. They are working on extending it to be comparable. Right now Ada isn't well suited for programs that do lots of runtime allocation.

[–] RagingHungryPanda@lemm.ee 2 points 1 day ago* (last edited 1 day ago)

This never became a real language, but years ago Microsoft Research work on Project Midori, an attempt to make a managed programming language suitable for an operating, and the operating system to go with it. It was based off of C# and heavily modified. Joe Duffy at Microsoft Research wrote some good blogs on it, about approaches they took and why, problems they ran in to, etc.

https://joeduffyblog.com/2015/11/03/blogging-about-midori/

[–] lukstru@lemmy.world 8 points 2 days ago

zig, kinda. You still have to manually manage memory, but for smaller applications there is e.g. the ArenaAllocator, which does everything for you. zig has a few nice features that make it just easier to manage memory. Keep in mind that it is not at release level and will change, but it's already functional.

[–] timlyo@kbin.earth 9 points 2 days ago

Ada comes to mind, goes further than Rust in this regard.

[–] Kidplayer_666@lemm.ee 5 points 2 days ago (1 children)

I mean Java and Python ig? But those don’t really count, do they?

[–] UnfortunateShort@lemmy.world 1 points 2 days ago

As someone who has used Python and Rust professionally, I'd say Python, while memory-safe, lacks proper type-checking. I don't know if there are alternatives, but Pylance is straight up trash. Also Python's performance doesn't even compare to Rust. It's a great scripting/programming language, but I think it's use cases are vastly different.

I'd say whether Python counts depends a lot on what you want to do. I have a very strong opinion on using Python for complex projects. When it comes to performance, it's suitable as nothing more than glue-code.

[–] Kyrgizion@lemmy.world 4 points 2 days ago

I'd say Malbolge is pretty safe. I'd like to see someone crack a system using that.

[–] z3rOR0ne@lemmy.ml 1 points 2 days ago

I'm new to C and Rust, but from what I can gather, C and C++ are not memory safe by default because they require the programmer to manually allocate memory onto the heap as needed and then free said memory, and then remember not to use said freed memory again. There is a lot of "ceremony" around this and it's easy to make mistakes, which result in memory leaks, use after free errors, undefined behavior, security bugs, etc.

Most high level languages (Python, JavaScript, Java, Golang, etc.) use a garbage collector (also known by the acronym, GC), which very essentially looks for when memory can be freed within your program and performs this allocation/deallocation ceremony for you. There are, however, disadvantages to running a garbage collector. Running a garbage collector costs allocation of memory itself, and it is not inherently efficient (depending on how the garbage collector itself was implemented).

Rust's solution to this utilizes techniques built around the concept of memory ownership, which is enforced using what is known as the Borrow Checker. In essence, Rust prevents memory leaks by ensuring that a variable (i.e. a reference to a value stored in memory) cannot be utilized in multiple different scopes of the program, but rather must be either copied (a new variable with the same value stored in a different space of memory), or the variable must be passed directly to the new scope, afterwards which it cannot be utilized outside of the scope it was passed to (hence the term of it being "borrowed".)

Im not sure, but I believe this is why Rust is more efficient than a garbage collected language because the techniques used in garbage collection can be computationally and resource intensive. These techniques include tracing, reference counting, and escape analysis.

This is as opposed to Rust's memory management model, which prevents memory leaks by explicit memory allocation of mutable variables which prevents the presence of dangling pointers being left within the codebase.

From my little experience working with both C and Rust, as well as languages like JS/TS and Python, is that Rust cuts the difference between the low level languages like C and the GC languages like Python in that you don't have to manually allocate and deallocate memory every time you need to use the heap, but you need to keep track of which scope owns which memory at any given time, otherwise the compiler (and hopefully long before that, your linter) will complain at you and prevent you from even compiling a binary in the first place.

Lastly, a point of interest. C++ has an optional borrow checker of sorts in its use of RAII.

Hope this helps sort things out a bit. Really there aren't many memory safe languages that use the ownership model. All GC languages are memory safe (assuming the GC is implemented well), but the computational cost of that memory safety is higher than with Rust.

In short, Rust's Ownership model makes Rust one of the few memory safe languages that is nearly as efficient as manually memory allocated languages like C, while still memory safe like with GC languages.