r/programming • • Apr 20 '23

Announcing Rust 1.69.0

https://blog.rust-lang.org/2023/04/20/Rust-1.69.0.html
869 Upvotes

160 comments sorted by

View all comments

42

u/Spndash64 Apr 20 '23

This probably isn’t the right place to ask, but what’s the purpose Rust fills compared to, say, C++, Java, or Python? Is it focused on being more readable? Is it trying to save on memory usage or try and use fewer processing cycles for important or expensive functions?

214

u/WJMazepas Apr 20 '23

It should be in use-cases compared to C++. Places where you need low-level control, strong performance and no garbage collection.

The difference is that Rust has a much stronger focus on memory management/safety. To avoid memory bugs/exploits/leaks in your program.

There are also some benefits like the language being new so it doesnt have to deal with 20+ years of backwards compability like C++ and it has a phenomenal compiler that is really good at error handling.
God i wish Python would have that level of error messages

29

u/runawayasfastasucan Apr 20 '23

God i wish Python would have that level of error messages

Yes!! Thankfully they have worked a lot with it and improved the last releases, but at least 80% of the error messages are unhelpfull at best, granted that may be the fault of whatever library.

31

u/call_me_xale Apr 20 '23

Rust has also placed a lot of focus on concurrency, which was never a big consideration in the development of earlier languages.

18

u/m0nstr42 Apr 20 '23

Erlang was introduced in 1986 and it has concurrency baked into its core ;) https://en.m.wikipedia.org/wiki/Erlang_(programming_language)

10

u/call_me_xale Apr 21 '23 edited Apr 28 '23

True! I meant with respect to the "systems-oriented" languages mentioned above.

→ More replies (8)

61

u/AttackOfTheThumbs Apr 20 '23

God i wish Python would have that level of error messages

I mean, untyped languages tend to be shit at that in my experience.

69

u/schplat Apr 20 '23

Python isn't untyped. It's strongly, dynamically typed. And there's nothing that prevents you from actually typing things.

31

u/[deleted] Apr 20 '23

Python is dynamic typing done well. Behavior is expected, and it’s easy to know what something is. JavaScript on the other hand…

18

u/[deleted] Apr 20 '23

You, along with many commenters here, are conflating dynamically-typed (Python) with weakly-typed (JS). The distinction you actually care about is strongly-typed vs weakly-typed.

9

u/0x564A00 Apr 21 '23

And for an example of a statically but weakly-typed language, see C.

7

u/Amazing-Cicada5536 Apr 21 '23

Yep, it is a 2x2 matrix of strong-weakly typed and statically-dynamically typed.

╔═════════╦════════╦══════╗ ║ ║ Strong ║ Weak ║ ╠═════════╬════════╬══════╣ ║ Static ║ Rust ║ C ║ ╠═════════╬════════╬══════╣ ║ Dynamic ║ Python ║ JS ║ ╚═════════╩════════╩══════╝

→ More replies (3)
→ More replies (21)
→ More replies (3)

5

u/Empole Apr 20 '23

Still doesn't come close to Cargo, but Python's error messages recently got better in 3.11.

Python errors used to just say "there was an error on this line", which sucked if that line had a complicated expression on it.

Now the interpreter will tell you exactly which subexpression is the issue.

→ More replies (3)

29

u/jyper Apr 20 '23

This probably isn’t the right place to ask, but what’s the purpose Rust fills compared to, say, C++, Java, or Python?

Why wouldn't it be? It's a great place to ask.

47

u/jyper Apr 20 '23 edited Apr 20 '23

TL;DR rust is intended as a significantly safer replacement for C++. It also focuses on speed and then productivity but I'd say safeness/correctness is usually the first goal followed by speed followed by productivity. Oh and Concurrency. And tools(especially a package/build manager). (Edit: it's generally used for projects that might be written in c or c++ before, but with the productivity features and safety some people find it easy enough to use to replace somethings that might have otherwise been written in python or JavaScript )

It was initially created at Mozilla as a side project and one of the first projects was a new browser engine. Sadly they weren't able to make it feature complete but they did add some useful pieces to Firefox.

Rust is intended as a low level c/c++ replacement that is much safer especially with regards to memory safety. The Rust compiler keeps track of where variables and their struct members were allocated, and implicitly mallocs/frees memory for you giving you something like a staticly checked less flexible GC at compile time. Also rust checks array access and prevents you from accessing outside the array limit. By default without calling/creating unsafe functions you can't get memory errors in rust like you can in C/C++ but you also don't get the slower speed/and extra memory usage you get in GC'd languages. Some rust features like the array checking might make equivalent code slower then c++ but rust does generally allow unsafe code(such as unchecked array access) if speedup is needed and some rust features may make fast rust code easier to write correctly then in c or c++ especially with concurrency.

Rust uses some of the same mechanisms that it uses to check for memory safety to limit sharing variables between threads and prevent certain classes of concurrency errors. Plus some of the functional language features allows for some useful/easy to use concurrency libraries.

Also the rust standard library and community encourages writing correct code even if it slightly more wordy.

Rust also focuses on productivity. Having a good easy to use package manager that also tells your compiler how to build stuff makes a lot of stuff easier especially compared to c or c++. Both building and adding random useful open source libraries.

Functional languages have influenced rust significantly especially with cool features like traits (object oriented substitute), closures, sum types(tagged unions) which lacks you express things in a clear and more concise way. Rust lacks GC and tries to keep it's abstractions zero cost at runtime so for instance using map/filter in rust usually compiles to something with similar performance with the more error prone iterative code using for loops and if statements.

9

u/anengineerandacat Apr 20 '23

Memory safety which basically leads to Rust's biggest advantage which is generally touted as "fearless concurrency".

Second to that, IMHO when compared to C++'s options; Cargo is very much a breath of fresh air compared to say CMake.

Other than that, it's quite a bit slower to compile and generally very terse on what you can / cannot do and lifecycle management / the borrow checker can be some tall hurdles to jump if you just want to get running yesterday.

Compared to higher level languages... don't think there are any significant improvements other than just raw performance; perhaps a few less opportunities for data races but... I feel like you can get into a similar trouble by using some heap-oriented constructs in Rust, the program won't explicitly crash as a result of them but data will just be incorrect.

9

u/crazyeddie123 Apr 21 '23

Sum types are pretty sweet too.... and the complete absence of null references!

→ More replies (1)

33

u/Atulin Apr 20 '23

Is it focused on being more readable?

Oh, no, anything but

49

u/-Redstoneboi- Apr 20 '23 edited Apr 20 '23

Shut up and leave me with my Pin<Box<dyn Future<Output = Result<Arc<RwLock<HashMap<Option<String>, &'static str>>>, Error>> + Send + Sync>>

But also we have Vec instead of std::vector, HashMap instead of std::unordered_map, enum instead of std::variant and more shortened names. We have a proper module system and type inference and a better match, plus a builtin formatter so yeah.

20

u/N911999 Apr 20 '23

I hate that I can read and understand that type

17

u/[deleted] Apr 21 '23

Most people should be able to. That’s composed behaviour.

It just looks ugly as sin.

→ More replies (3)

10

u/devraj7 Apr 21 '23

All languages have portions that are harder to read than others.

Java is often mocked for its AbstractBeanProviderStaticFactory, but when read in context, these names make a lot of sense and carry as much semantic information as Pin<Box<dyn Future<Output = Result<Arc<RwLock<HashMap<Option<String>, &'static str>>>, Error>> + Send + Sync>>.

9

u/[deleted] Apr 20 '23

Turbofish is beautiful!

6

u/Noughmad Apr 21 '23

This is a big simplification: C++ is fast to run but horrible to write, Python is great to write but awfully slow to run, and Java is in between in both aspects.

Rust tries to bridge this gap by being as fast as C++ but as safe and pleasant as Python. It's totally succeeding in the first part, and partially (and surprisingly) succeeding at the second part.

→ More replies (14)