user avatar
Mara
@m_ou_se
Rust dev, Electronics engineer, Author, @rustlang Library team lead, ADHD, Polyamorous, Lesbian, She/Her
Delft, Nederland
Joined April 2009
  • Pinned
    user avatar
    You should follow me on the other app: mara.bsky.social ๐Ÿฆ‹
  • user avatar
    Got a new laptop with the wrong power plug. But nothing a little `unsafe` block can't solve. ๐Ÿ˜Œ ๐Ÿฆ€ โšก๏ธ
    A top-down picture showing a european power strip and a UK power plug on a white background. Three wires with crocodile clips connect the prongs of the power plug to the relevant parts of a power socket. Overlayed on the picture in large letters is the text "unsafe" followed by an opening bracket on top, and a closing bracket at the bottom of the picture, encapsulating this horrible set-up.
  • user avatar
    "Nobody uses Rust in production", he emailed (with Thunderbird) from his laptop (running Windows) to a mailing list (on AWS, behind Cloudflare), which I then read (in Firefox) on my phone (running Android) after clicking a link (in Discord) to the list archive (served by Fastly).
  • user avatar
    ๐Ÿฆ€๐Ÿ“• All chapters of my book, Rust Atomics and Locks, are now freely available online: marabos.nl/atomics/ Enjoy! โœจ
  • user avatar
    โš›๏ธ๐Ÿ“‹ I made an overview of the ARMv8 and x86-64 machine instructions for all the common atomic operations:
    Six tables showing the machine instructions for the atomic operations on ARMv8, ARMv8.1 and x86-64, for non-atomic, relaxed, acquire, release, acquire plus release, and sequentially consistent memory ordering.

The six tables are labeled: load, store, swap, fetch op, compare exchange, fence.

The full contents don't fit in the alt text, but here are the main takeaways:

On x86, in each table, the instructions are the same for all (atomic) memory orderings, except seqcst store using a XCHG instruction instead of a MOV, and seqcst fence using MFENCE instead of nothing.

On ARM, the acquire plus release instructions are equivalent to the sequentially consistent instructions in all cases.

On ARMv8, the swap, fetch op and strong compare exchange are implemented as loops using the LDXR and STXR instructions. But on ARMv8.1, they are all implemented as a single instruction.

On x86, fetch op is implemented with a lock prefix for some operations, and as a CMPXCHG loop for some others.
  • user avatar
    ๐Ÿš Brace expansion in shells like Bash and Zsh can be very useful to avoid repeating yourself. โœจ For example, if you want to do: $ mv some_long_file_name.txt some_long_file_name.rs You can write this instead: $ mv some_long_file_name.{txt,rs} Here's some ways to use braces:
    # {Bash,Zsh} brace expansion

# Brace expansion:
$ echo x{Hello,World}x
= echo xHellox xWorldx

# With an empty string:
$ cp some_file{,.bak}
= cp some_file some_file.bak

# In a path:
$ mv x/{before,after}/asdf/file
= mv x/before/asdf/file x/after/asdf/file

# Number range:
$ rm d{01..20}/file
= rm d01/file d02/file d03/file d04/file ... etc.

# Multiple braces:
$ touch {a,b,c}.{rs,cpp}
= touch a.rs a.cpp b.rs b.cpp c.rs c.cpp

# Nested braces:
$ git add {main,x{1,2}}.rs
= git add main.rs x1.rs x2.rs

# Letter range:
$ cat {a..f}.txt
= cat a.txt b.txt c.txt d.txt e.txt f.txt
  • user avatar
    ๐Ÿ†•๐Ÿฆ€ A new exciting @rustlang feature coming up in Rust 1.58: format argument capturing! ๐ŸŽ‰โœจ Starting in Rust 1.58, which will be released as stable on January 13, you can refer to variables from within format strings: let name = "world"; println!("Hello, {name}!"); Examples:
    let name = "world";

println!("Hello, {name}!");
    let num = 123;

let s = format!("{num:#x}");

assert_eq!(s, "0x7b");
    use log::info;

let v = vec![1, 2, 3];

info!("numbers: {v:?}");
    fn f(n: i32) {
    assert!(n > 5, "n is {n}, but should be higher than five");
  
    ..
}
  • user avatar
    ๐Ÿฆ€๐Ÿ“• I just finished writing the ninth chapter of my ten-chapter #rustlang book! Almost done! โœจ๐Ÿฅณ If everything goes as planned, it should be available before the end of the year, so you can all read it during the holidays. ๐Ÿ˜Š
    Screenshot of the O'Reilly website showing a book cover of "Rust Atomics and Locks - Low-Level Concurrency in Practice". The cover has a black and white kodiak bear and a sticker showing "Early Release - raw & unedited". The text next to the cover reads as  follows:

Rust Atomics and Locks

by Mara Bos
Released November 2022
Publisher(s): O'Reilly Media, Inc.
ISBN: 9781098119423

In this practical book, Mara Bos, leader of the Rust library team, helps Rust programmers of all levels gain a clear understanding of low-level concurrency. You'll learn everything about atomics and memory ordering and how they're combined with basic operating system APIs to build common primitives like mutexes and condition variables. Once you're done, you'll have a firm grasp of how Rust's memory model, the processor, and the roles of the operating system all fit together.
  • user avatar
    ๐Ÿ†•๐Ÿฆ€ About an hour ago, @rustlang 1.65.0 was released. As is tradition, here's a thread with some of the highlights. ๐Ÿงต 1/10
  • user avatar
    ๐Ÿฆ€๐Ÿ“• Did I tell y'all I've been working on a book?! ๐Ÿคฉ Title: Rust Atomics and Locks It's not finished yet! But it will be, later this year! โœจ
    Screenshot of the O'Reilly website showing a book cover of "Rust Atomics and Locks - Low-Level Concurrency in Practice". The cover has a black and white kodiak bear and a sticker showing "Early Release - raw & unedited". The text next to the cover reads as  follows:

Rust Atomics and Locks

by Mara Bos
Released November 2022
Publisher(s): O'Reilly Media, Inc.
ISBN: 9781098119423

In this practical book, Mara Bos, leader of the Rust library team, helps Rust programmers of all levels gain a clear understanding of low-level concurrency. You'll learn everything about atomics and memory ordering and how they're combined with basic operating system APIs to build common primitives like mutexes and condition variables. Once you're done, you'll have a firm grasp of how Rust's memory model, the processor, and the roles of the operating system all fit together.
  • user avatar
    โœจ Need to quickly plot something in @rustlang? Just import the excellent Matplotlib from Python! ๐Ÿฆ€ ๐Ÿ’› ๐Ÿ ๐Ÿ“Š
    A screenshot of a desktop with two windows and a pink background.

On the left, a terminal showing the source of src/main.rs and running cargo r --quiet.

On the right, a window named "Figure 1" showing two plots. The top one shows a lot of dots, and the bottom one is a histogram showing mostly a very big spike around 1270 ฮผs.

The source code of src/main.rs:

use inline_python::python;
use std::{
  thread::sleep,
  time::{Duration, Instant},
};

fn main() {
  let mut data = Vec::new();

  for _ in 0..10000 {
    let start = Instant::now();
    sleep(Duration::from_millis(1));
    let duration = start.elapsed();
    data.push(duration.as_secs_f64() * 1e6);
  }

  python! {
    import matplotlib.pyplot as plt

    plt.suptitle("Time taken to sleep 1 millisecond")

    plt.subplot(2, 1, 1)
    plt.title("measurements")
    plt.plot('data, ".")
    plt.ylabel("ฮผs")

    plt.subplot(2, 1, 2)
    plt.title("histogram")
    plt.hist('data, bins=250)
    plt.xlabel("ฮผs")

    plt.show()
  }
}
  • user avatar
    ๐Ÿฆ€ The standard library of @rustlang 1.63 ships with a long-awaited feature: scoped threads! Unlike thread::spawn(), this new feature allows threads to borrow local variables, instead of only 'static ones. ๐ŸŽ‰ (Rust 1.63 will be released on August 11th.) doc.rust-lang.org/nightly/std/thโ€ฆ
    let mut a = vec![1, 2, 3];
let mut x = 0;

std::thread::scope(|s| {
    s.spawn(|| {
        println!("hello from the first scoped thread");
        // We can borrow `a` here.
        dbg!(&a);
    });
    s.spawn(|| {
        println!("hello from the second scoped thread");
        // We can even mutably borrow `x` here,
        // because no other threads are using it.
        x += a[0] + a[2];
    });
    println!("hello from the main thread");
});

// After the scope, we can modify and access our variables again:
a.push(4);
assert_eq!(x, a.len());
  • user avatar
    ๐Ÿฆ€๐Ÿ“• Here's a sneak preview of my book, Rust Atomics and Locks ๐Ÿ“– It's already available for pre-order! It should ship in December, so you can all read it during the holidays. โœจ amazon.com/Rust-Atomics-Lโ€ฆ
    Two pages from the preface of my book, showing a section titled: Overview of the Chapters

The full text doesn't fit in Twitter's alt text. The full text can be found on https://tinyurl.com/2ymfx58u

This book consists of ten chapters. Here's what to expect from each chapter, and what to look forward to:

Chapter 1 โ€” Basics of Rust Concurrency
Chapter 2 โ€” Atomics
Chapter 3 โ€” Memory Ordering
Chapter 4 โ€” Building Our Own Spin Lock
Chapter 5 โ€” Building Our Own Channels
Chapter 6 โ€” Building Our Own "Arc"
Chapter 7 โ€” Understanding the Processor
Chapter 8 โ€” Operating System Primitives
Chapter 9 โ€” Building Our Own Locks
Chapter 10 โ€” Ideas and Inspiration
  • user avatar
    Hey @rust_foundation, maybe don't promote ponzi schemes. As a reminder to everyone: the Rust Foundation is entirely separate from the Rust Project. They do not represent us, and they do not involve us in decisions like this.
    Rust Foundation Silver Member @_parastate provides insight into how Rust can help reduce blockchain energy consumption. Read the piece: buff.ly/3MUzELf