News for hackers. Stuff that matters.
Re:From Zero to Main(): Bare Metal Rust by privethedge on Tuesday December 17, 2019 @08:18PM (#21820868)
This is great and I love Rust, but

> However, when working directly with the hardware, which has no knowledge of Rust’s guarantees, it is necessary to work in Rust’s unsafe mode, which allows some additional behaviors, but requires the developer to uphold certain correctness guarantees manually.

If the whole code will be wrapped in unsafe blocks, then what is the point of using Rust?

Re:From Zero to Main(): Bare Metal Rust by hackcasual on Tuesday December 17, 2019 @08:28PM (#21820914)
This example is a bit of an outlier since it's so small, but a larger program is going to need proportionally smaller amounts of unsafe code. Remember, just because it's marked unsafe, doesn't mean it's not rust. It's just that for doing low level micro-controller type things, you're explicitly addressing parts of RAM (or memory mapped I/O), and that means creating your own pointer.
Re:From Zero to Main(): Bare Metal Rust by chubs on Tuesday December 17, 2019 @10:10PM (#21821420)
So is it the case that you only need 'unsafe' for some of your codebase where you're eg bit-banging, or using peripherals, or whatever - but the majority of your logic is running in normal 'safe' mode?
Re:From Zero to Main(): Bare Metal Rust by PudgePacket on Tuesday December 17, 2019 @10:58PM (#21821639)
Some amount of unsafe is impossible to avoid for doing certain things. Rust unsafe allows developers to focus their memory auditing efforts, rather than existing languages where _everywhere_ must be checked.

Here's an example. The Rust standard library has unsafe code to do things like allocate memory and read from files. However, it provides a safe abstraction that my code can use "safely" and be confident I won't encounter any memory errors.

Or if you're writing some kind of concurrent data structure, you want to be doing pointer and memory shenanigans for efficiency, but you'd make a safe API for consumers of the data structure.

I think a lot of people are scared initially seeing something like "unsafe". It's kind of equivalent to just regular C/C++ land... It doesn't automatically mean "The program will now eat your memory".

Re:From Zero to Main(): Bare Metal Rust by littlestymaar on Tuesday December 17, 2019 @10:43PM (#21821569)
That's right.

For instance, in the redox-os kernel, there is less than 100 places where unsafe is needed : https://doc.redox-os.org/book/introduction/unsafes.html.

Re:From Zero to Main(): Bare Metal Rust by mlindner on Tuesday December 17, 2019 @10:53PM (#21821610)
And to be more specific you'd ideally write an interface implementation that does that bit banging for you such that you call those functions from your safe code.
Re:From Zero to Main(): Bare Metal Rust by PudgePacket on Tuesday December 17, 2019 @09:01PM (#21821077)
Ecosystem, build tools, ergonomics, familiarity, etc. Rust is more than just handrails for C code.
Re:From Zero to Main(): Bare Metal Rust by cttet on Wednesday December 18, 2019 @02:17AM (#21822618)
Why not? There are people who are more familiar with Rust than C/C++, why shouldn't they use a more familiar language?
Re:From Zero to Main(): Bare Metal Rust by baq on Wednesday December 18, 2019 @04:36AM (#21823266)
at that point you're getting the worst of both worlds: no safety guarantees and a very complex language.
Re:From Zero to Main(): Bare Metal Rust by paavohtl on Wednesday December 18, 2019 @04:45AM (#21823305)
Unsafe Rust has significantly more safety guarantees than C or C++. It doesn't disable the type system, bounds checking, ownership tracking or borrow checking.
Re:From Zero to Main(): Bare Metal Rust by CaptainMarvel on Thursday December 19, 2019 @03:04AM (#21832932)
What does Unsafe Rust disable then?
Re:From Zero to Main(): Bare Metal Rust by steveklabnik on Thursday December 19, 2019 @10:27AM (#21835668)
It does not disable, it enables. Unsafe doesn't turn anything off, it adds new capabilities instead. https://doc.rust-lang.org/nomicon/what-unsafe-does.html
Re:From Zero to Main(): Bare Metal Rust by IshKebab on Wednesday December 18, 2019 @05:14AM (#21823491)
You don't have to do everything in unsafe blocks.