Is Rust Used Safely by Software Developers?
I&#x27;m not a Rust programmer, so be gentle with me.<p>What worries me is that when it comes to safety, surely the chain is as strong as the weakest link? You declare one thing unsafe, and then suddenly all bets are off. And maybe you don&#x27;t even know that what you&#x27;re doing is unsafe.<p>But here&#x27;s an example I pulled from github, which blinks a Raspberry Pi Pico:<p><pre><code>    &#x2F;&#x2F; Set GPIO25 to be an output (output enable is set)
    p.SIO.gpio_oe_set.write(|w| unsafe {
        w.bits(1 &lt;&lt; 25);
        w
    });
</code></pre>
Right, but what if you&#x27;re fiddling with registers that might require atomic operations? Lines such as this seem to be saying &quot;ignore all that, write to the register anyway.&quot;<p>Fearless concurrency, you say: are you sure? How do you know?<p>Suppose you have an ISR (interrupt service routine) that wants to toggle a GPIO pin. From whence does it borrow the privilege to set the pin?<p>Surely, at the end of the day, microcontrollers can be characterised as large state machines with global state. That&#x27;s what they ARE. It&#x27;s their inherent nature, which you cannot abstract away without it leaking somewhere (at least to do anything useful, anyway).<p>At least with C&#x2F;C++ you KNOW you&#x27;ve got a problem, and have to reason about the problem carefully. With Rust, you THINK you&#x27;ve solved the problem, but have you?<p>&quot;Change My View&quot;, as they say on Reddit.<p>*Update 1* OK, I&#x27;m going to press my point on this. I&#x27;m looking at the Embedded Rust Book (please remember, I&#x27;m not a Rust programmer) at 
<a href="https:&#x2F;&#x2F;docs.rust-embedded.org&#x2F;book&#x2F;concurrency&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;docs.rust-embedded.org&#x2F;book&#x2F;concurrency&#x2F;index.html</a><p>I&#x27;m considering the issue of interrupts here.<p>&quot; Instead, our interrupt handlers might be called at any time and must know how to access whatever shared memory we are using. At the lowest level, this means we must have statically allocated mutable memory, which both the interrupt handler and the main code can refer to.&quot;<p>&quot;In Rust, such static mut variables are always unsafe to read or write, because without taking special care, you might trigger a race condition, where your access to the variable is interrupted halfway through by an interrupt which also accesses that variable.&quot;<p>RIGHT, but that means that Rust gives me no better assurances than C&#x2F;C++. Here&#x27;s their code:<p><pre><code>    static mut COUNTER: u32 = 0;
    
    #[entry]
    fn main() -&gt; ! {
        set_timer_1hz();
        let mut last_state = false;
        loop {
            let state = read_signal_level();
            if state &amp;&amp; !last_state {
                &#x2F;&#x2F; DANGER - Not actually safe! Could cause data races.
                unsafe { COUNTER += 1 };
            }
            last_state = state;
        }
    }
    
    #[interrupt]
    fn timer() {
        unsafe { COUNTER = 0; }
    }
</code></pre>
Then is says &quot;Can you spot the race condition? The increment on COUNTER is not guaranteed to be atomic&quot;<p>It then solves the problem by showing how to introduce a critical section.<p>So at the end of the day it&#x27;s just C with different syntax, and a lot more hoopla. You&#x27;ve made everything mutable and unsafe. Exactly the kind of thing you were trying to avoid in the first place.