News for hackers. Stuff that matters.
Re:Rust 1.63 by CJefferson on Thursday August 11, 2022 @12:42PM (#32428383)
Personally, I use Rust as a "better C++".

Ways in which it is better:

* A decent package manager

* "Safe by default" (you have to use 'unsafe' to turn off the safety features), unlike C++ which is "unsafe by default" (v[i] is undefined behaviour if i is out of bounds, for all of C arrays, std::arrays and std::vectors).

* Really easy to do multithreading -- I had reached the point with C and C++ where I was of the opinion it is "almost impossible" to write large thread-safe programs, and all communication should be between processes with pipes, wherever reasonable. In Rust I again feel I can safely and productively write multi-threaded code.

The things Rust doesn't let you do (throw pointers around everywhere) can get a little annoying, but I find the extra safety increases my productivity enough that it's worth the tradeoff.

Re:Rust 1.63 by dralley on Thursday August 11, 2022 @04:03PM (#32431033)
>(you have to use 'unsafe' to turn off the safety features)

"unsafe" doesn't turn off any safety features, it just allows you to do additional things which the base language does not, like:

* use raw pointers

* use mutable static globals

* access fields of unions

https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#unsa...

All the rules of safe Rust still apply inside of unsafe blocks. The borrow checker still works, etc.

Re:Rust 1.63 by tialaramex on Thursday August 11, 2022 @04:17PM (#32431195)
You didn't say otherwise, but this is such a frequent misunderstanding from people who don't write Rust, or write only safe Rust (which is fine) that it's worth pointing out the unsafe keyword and Rust's choice to be "safe by default" are not directly related. Culturally obviously they come from the same place, but for example:

v[i] is bounds checked in Rust even in unsafe. The unsafe "super powers" don't include "magically now indexing has no bounds checks" but because Rust's get function v.get_unchecked(i) is marked unsafe‡ you would need to mark code unsafe to use that function and that function doesn't have bounds checks.

If you write:

  unsafe {
     println!("{}", v[i]);
  }
The compiler will point out that unsafe isn't doing anything for you here, v[i] is safe, marking it unsafe is just a waste of everybody's time.

C++ doesn't have bounds checked array access for its abandoned built-in arrays, but it does (these days) have bounds checked access for std::array and std::vector as a separate member function, however because they aren't the default few C++ programmers use them even where they undoubtedly should. This is an ergonomics issue, it's just easier to write needlessly unsafe C++.

‡ This said just get() before I edited it, but Steve corrected me, see below.

Re:Rust 1.63 by int_19h on Thursday August 11, 2022 @07:17PM (#32433029)
C++ has had std::vector::at() - which checks for out-of-bounds indices - since C++98. The reason why it's not used all that much in practice is because indexing is relatively rare - most of the time, you just iterate over collections, or otherwise use iterators rather than raw indices. And there's no ability to opt into bounds checking for C++ iterators in the standard - implementations usually provide some kind of checked iterators for debug builds, but performance is such that you'd never want that enabled for release.
Re:Rust 1.63 by tialaramex on Friday August 12, 2022 @06:33AM (#32436584)
> indexing is relatively rare

This is also true in Rust for the same reason, arguably more true since Rust's built-in array type knows how big it is - but the observation in both languages programmers who want indexing write v[i] because that's the natural way to express what you meant, what's different isn't the frequency of need, only the language defaults.

> performance is such that you'd never want that enabled for release.

Let's make sure it's correct before we start worrying about performance

https://twitter.com/magdraws/status/1551612747569299458

Re:Rust 1.63 by pjmlp on Friday August 12, 2022 @02:19AM (#32435203)
I always keep them enabled for release, performance is good enough.

Most devs should learn to use profilers.

Re:Rust 1.63 by int_19h on Saturday August 13, 2022 @04:28PM (#32453478)
In what implementation, and what guarantees does its debug iterators provide?
Re:Rust 1.63 by pjmlp on Sunday August 14, 2022 @12:36AM (#32456366)
The one from your employer, Visual C++.
Re:Rust 1.63 by int_19h on Monday August 15, 2022 @04:47AM (#32467292)
If you mean _ITERATOR_DEBUG_LEVEL=1, they're about 4x slower for iteration. I suppose "good enough" is subjective, but if this much is acceptable, I'd rather use C# - it's in the same ballpark.
Re:Rust 1.63 by steveklabnik on Thursday August 11, 2022 @04:42PM (#32431469)
Teeeny tiny note

> because Rust's get function v.get(i) is marked unsafe

You mean get_unchecked. get is safe, and returns an Option, so that None can be returned if something is out of bounds.

Re:Rust 1.63 by tialaramex on Thursday August 11, 2022 @05:22PM (#32431878)
Doh! I even had the "get_unchecked()" function in front of me on the screen and wrote get() anyway. Thanks Steve.
Re:Rust 1.63 by steveklabnik on Thursday August 11, 2022 @05:34PM (#32431997)
No problem. Thanks for explaining what unsafe does and doesn't do; you're 100% right that this is a common misconception that comes up often.
Re:Rust 1.63 by pjmlp on Friday August 12, 2022 @02:18AM (#32435199)
Kind of true, except operator[]() does indeed do bounds checking across all major compilers when compiling in debug mode, and all of them have switches to keep the same behavior in release mode as well.
Re:Rust 1.63 by CJefferson on Thursday August 11, 2022 @05:16PM (#32431805)
You are right, I did know this but it's worth clarifying. Thanks.