News for hackers. Stuff that matters.
Re:Thoughts on Go vs. Rust vs. Zig by ekropotin on Thursday December 04, 2025 @05:45PM (#46154232)
I pretty new to Rust and I’m wondering why global mutables are hard?

At first glance you can just use static variable of a type supporting interior mutability - RefCell, Mutex, etc…

Re:Thoughts on Go vs. Rust vs. Zig by written-beyond on Thursday December 04, 2025 @05:51PM (#46154295)
I don't think it's specifically hard, it's more related to how it probably needed more plumbing in the language that authors thought would add to much baggage and let the community solve it. Like the whole async runtime debates
Re:Thoughts on Go vs. Rust vs. Zig by umanwizard on Thursday December 04, 2025 @06:19PM (#46154616)
> I pretty new to Rust and I’m wondering why global mutables are hard?

They're not.

  fn main() {
      unsafe {
          COUNTER += 1;
          println!("COUNTER = {}", COUNTER);
      }
  
      unsafe {
          COUNTER += 10;
          println!("COUNTER = {}", COUNTER);
      }
  }
Global mutable variables are as easy in Rust as in any other language. Unlike other languages, Rust also provides better things that you can use instead.
Re:Thoughts on Go vs. Rust vs. Zig by raggi on Thursday December 04, 2025 @07:49PM (#46155481)
People always complain about unsafe, so I prefer to just show the safe version.

  use std::sync::Mutex;

  static LIST: Mutex<Vec<String>> = Mutex::new(Vec::new());

  fn main() -> Result<(), Box<dyn std::error::Error>> {

      LIST.lock()?.push("hello world".to_string());
      println!("{}", LIST.lock()?[0]);

      Ok(())
  }
Re:Thoughts on Go vs. Rust vs. Zig by hu3 on Friday December 05, 2025 @05:46PM (#46168408)
This is completely different from the previous example.

It doesn't increment anything for starters. The example would be more convoluted if it did the same thing.

And strings in rust always delivers the WTFs I need o na Friday:

    "hello world".to_string()
Re:Thoughts on Go vs. Rust vs. Zig by raggi on Friday December 05, 2025 @09:18PM (#46169945)

    use std::sync::Mutex;
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        static PEDANTRY: Mutex<u64> = Mutex::new(0);
        *PEDANTRY.lock()? += 1;
        println!("{}", PEDANTRY.lock()?);
        Ok(())
    }
Re:Thoughts on Go vs. Rust vs. Zig by hu3 on Saturday December 06, 2025 @04:36PM (#46176790)
Still different. Yours only increments once. Doesn't pass basic QA.

And declaring a static variable inside a function, even if in main, smells.

Re:Thoughts on Go vs. Rust vs. Zig by raggi on Saturday December 06, 2025 @05:23PM (#46177107)
OP you keep comparing to doesn't even declare the variable. I'm done with you.
Re:Thoughts on Go vs. Rust vs. Zig by umanwizard on Sunday December 07, 2025 @12:31PM (#46183395)
OP here, not showing how to declare the variable was an oversight on my part.

  static mut COUNTER: u32 = 0;
(at top-level)

If on 2024 edition, you will additionally need

  #![allow(static_mut_refs)]
Re:Thoughts on Go vs. Rust vs. Zig by raggi on Sunday December 07, 2025 @01:07PM (#46183658)
Yup, your example was fine, as I said I just wanted to show a safe method too :-)
Re:Thoughts on Go vs. Rust vs. Zig by masklinn on Friday December 05, 2025 @12:38AM (#46157152)
That is correct. Kinda. Refcell can not work because Rust considers globals to be shared by multiple threads so requires thread safety.

And that’s where a number of people blow a gasket.

Re:Thoughts on Go vs. Rust vs. Zig by masklinn on Friday December 05, 2025 @04:53AM (#46159167)
A second component is that statics require const initializers, so for most of rust’s history if you wanted a non-trivial global it was either a lot of faffing about or using third party packages (lazy_static, once_cell).

Since 1.80 the vast majority of uses are a LazyLock away.