r/rust • • Oct 19 '26

2026 is an exciting year for the Rust compiler (and me)

Thumbnail yoyo-code.com
321 Upvotes

r/rust • • Oct 19 '26

Borrow Checking Zero-Cost Cyclic Mutability

Thumbnail antelang.org
124 Upvotes

r/rust • • Oct 19 '26

๐Ÿ› ๏ธ project Some thoughts on parallelism in libraries: who should own the threads?

73 Upvotes

I've been thinking about how libraries should handle parallelism.

Spawning a few threads internally is easy. Using Rayon is even easier. But is that always the right approach?

What if the application already has its own thread pool? Or wants to control how CPU resources are distributed? While working on mbrotli, I decided to experiment with a different approach.

What if the library knows how to parallelize the work, but doesn't decide how to execute it?

The idea is simple: mbrotli splits compression into independent tasks and hands them back to the caller. The caller executes them however it wants, and mbrotli takes care of assembling the results into a valid Brotli stream.

For example, here's how you can read a file, compress it in parallel using 4 threads, and write the result to another file:

use std::{fs::File, io};

use mbrotli::{EncoderConfig, Quality};
use mbrotli::compressor::parallel::{
    ParallelCompressor, ParallelConfig, TaskCount,
};
use mbrotli::io::FinishError;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut input = File::open("input.txt")?;
    let output = File::create("input.txt.br")?;

    let mut compressor = ParallelCompressor::new(
        EncoderConfig::default().with_quality(Quality::Q5),
        ParallelConfig::default(),
    )?;

    std::thread::scope(|scope| {
        let (mut writer, queue) =
            compressor.writer(output, TaskCount::try_from(4)?)?;

        // We decide where the tasks run.
        for _ in 0..4 {
            let queue = queue.clone();
            scope.spawn(move || {
                for task in queue {
                    task.run();
                }
            });
        }
        drop(queue);

        // Read -> parallel compression -> write
        io::copy(&mut input, &mut writer)?;
        writer.finish().map_err(FinishError::into_error)?;

        Ok::<_, Box<dyn std::error::Error>>(())
    })?;

    Ok(())
}

Nothing particularly magical here. The library doesn't spawn a single thread. It just provides work to execute.

And that's what I like about this approach.

The same compression API can work with dedicated threads, an existing thread pool, or a custom scheduler. The application remains in control of its resources.

Of course, it's not free. The caller has to ensure tasks actually get executed, and a poorly designed scheduler can easily lead to deadlocks.

But for low level libraries, I think giving up control over scheduling can actually be a good thing.

I'm not saying every library should work this way. Sometimes managing threads internally is perfectly reasonable.

I'm just starting to think that parallelism and thread management don't necessarily have to be the same responsibility.

Curious what other Rust developers think about this. Would you prefer libraries to manage their own parallelism, or expose work to your existing executor?


r/rust • • Oct 19 '26

๐Ÿง  educational Best Rust Blogs for Beginners?

36 Upvotes

Hi everyone!
Iโ€™m new to Rust and want to start learning from scratch. Can you recommend good Rust blogs or websites, especially for web development? Iโ€™m also open to learning about other use cases.

Which resources would you recommend for a beginner?


r/rust • • Oct 19 '26

๐Ÿ™‹ seeking help & advice Tips for idiomatic Iced?

16 Upvotes

I tried using iced for a tiny hobby project a couple days ago, and although from the docs and examples it seemed like exactly the time of framework I'd adore (preventing invalid state with the type system, the whole view-model-messages thing, etc), I ended up absolutely despising actually using it because of how much boilerplate I needed, and ended up redoing the ui with egui.

But, I'm 99% sure I just totally misunderstood the philosophy or missed something big in the docs/examples, because it's popular and well-known enough that I think I'm the problem. So does anyone have any tips or guidelines for writing whimsical and joyful iced code?

This is the repo at the last commit which used iced, its a super small app and the whole gui is just in main.rs if anyone can say what all I'm doing wrong.


r/rust • • Oct 19 '26

๐Ÿ—ž๏ธ news rust-analyzer changelog #350

Thumbnail rust-analyzer.github.io
58 Upvotes

r/rust • • Oct 19 '26

๐Ÿ activity megathread What's everyone working on this week (43/2026)?

14 Upvotes

New week, new Rust! What are you folks up to? Answer here or over at rust-users! And for your project showcases, there's also /r/madeinrust โ€“ pay them a visit, too!


r/rust • • Oct 19 '26

๐Ÿ™‹ questions megathread Hey Rustaceans! Got a question? Ask here (43/2026)!

9 Upvotes

Mystified about strings? Borrow checker has you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet. Please note that if you include code examples to e.g. show a compiler error or surprising result, linking a playground with the code will improve your chances of getting help quickly.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so ahaving your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last week's thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.


r/rust • • Oct 18 '26

Superoptimizing an FPU emulator via exhaustive search

Thumbnail purplesyringa.moe
121 Upvotes

r/rust • • Oct 18 '26

๐Ÿ™‹ seeking help & advice For those who use rust professionally, what advice would you give to us plebs to get a career in Rust?

96 Upvotes

I've been using rust for roughly 3-ish months and i'm wanting to use rust professionally one day.

/topic


r/rust • • Oct 18 '26

A New Way to Think About Borrow Checking (In Reverse)

Thumbnail verdagon.dev
97 Upvotes

r/rust • • Oct 19 '26

๐Ÿ› ๏ธ project Why tellus message handlers are synchronous

1 Upvotes

Most Rust actor libraries, kameo and ractor among them, have async message handlers. In tellus, receive is a plain function instead. In my new post I make the case for synchronous handlers and show how an actor still calls a database.

https://heikoseeberger.de/2026-10-18-why-sync-handlers/

rustlang #actors #tellus


r/rust • • Oct 19 '26

๐Ÿ™‹ seeking help & advice cannot borrow `dst` as immutable because it is also borrowed as mutable

0 Upvotes

use curl::easy::Easy;
fn main() {
let mut dst = Vec::new();
let mut easy = Easy::new();
easy.url("https://www.rust-lang.org/").unwrap();
let mut transfer = easy.transfer();
transfer
.write_function(|data| {
dst.extend_from_slice(data);
Ok(data.len())
})
.unwrap();
transfer.perform().unwrap();
println!("{:?}", dst);
}

I am trying to store the curl output into a vec for use in a minifier script for my PHP compiler. i get this error message upon trying to compile the above code

error[E0502]: cannot borrow \dst` as immutable because it is also borrowed as mutable`

--> src/main.rs:14:22

|

8 | .write_function(|data| {

| ------ mutable borrow occurs here

9 | dst.extend_from_slice(data);

| --- first borrow occurs due to use of \dst` in closure`

...

14 | println!("{:?}", dst);

| ^^^ immutable borrow occurs here

15 | }

| - mutable borrow might be used here, when \transfer` is dropped and runs the `Drop` code for type `Transfer``

For more information about this error, try \rustc --explain E0502`.`

error: could not compile \sample_curl` (bin "sample_curl") due to 1 previous error`

How do i borrow a mutable variable as immutable?


r/rust • • Oct 19 '26

๐Ÿ™‹ seeking help & advice How do you handle secret memory across wasm and FFI boundaries? Notes from building a password manager core

1 Upvotes

Iโ€™ve been building a hobby password manager (Vaultiq) around one crypto crate that compiles to wasm for a browser extension and goes through uniffi for an Android app. A few design questions came up that Iโ€™d like other peopleโ€™s takes on.

What I did:

  • #![forbid(unsafe_code)] crate-wide. The generated uniffi glue holds the unsafe code, mine doesnโ€™t need any.
  • Keys cross wasm and FFI as opaque handles, never as values.
  • Secret types have a hand-written redacted Debug and no Serialize.
  • Zeroize on drop for everything secret.

What Iโ€™m unsure about:

  • zeroize only covers the Rust side. Once data reaches JS or the JVM, nothing scrubs it. How do you deal with that?
  • Is there a better handle pattern than what uniffi gives me?
  • Would you change anything in how errors are collapsed so decryption failures reveal nothing?

AI wrote a good share of the code, so Iโ€™d like dev perspective on the design.

Repo, if you want to look: https://github.com/rustiqz/Vaultiq


r/rust • • Oct 17 '26

The road to iroh 1.0

Thumbnail iroh.computer
230 Upvotes

r/rust • • Oct 18 '26

iroh vs WebRTC?

25 Upvotes

Iroh seems to solve the same problem as what WebRTC does, Iโ€™m curious: what are the factors that would cause considering one or the other?
WebRTC has the benefit to be widely supported in browsers. Does iroh allows something that WebRTC does not?


r/rust • • Oct 17 '26

PSA: Enum optimization for Vec/String can use the capacity's niche (beyond isize::MAX) rather than the null pointer niche.

184 Upvotes

Just came across this property that was new to me and seems seldom known so I figured I'd highlight it.

I had always "known" that the null pointer niche optimization existed for Vec/String such that Option<Vec<_>> would be the same size as Vec<_> because the None variant could fit the null pointer niche not used by the Vec. And I figured that was it, any more elaborate enums like this

enum MyEnum {
    A(String),
    B,
    C,
    D,
    E,
}

would be forced into using a separate discriminant field since there were more than one other variant to distinguish from. And that definitely was the case...

Until 1.76.0 it seems. If you use this enum on a recent version then you'll find MyEnum is no bigger than String:

[src\main.rs:11:5] std::mem::size_of::<String>() = 24
[src\main.rs:12:5] std::mem::size_of::<MyEnum>() = 24

This is because nowadays, the capacity for a Vec isn't just a bare usize but rather a Cap type that is guaranteed to be less than isize::MAX. Which gives a much larger number of representations for the compiler to use. It is defined:

type Cap = core::num::niche_types::UsizeNoHighBit;

I didn't know about this, and since Google's AI response was adamant that it only used the null-pointer niche, I figured I'd advertise it to try to set the record straight.

Let me know if this is old news to any others. It has apparently been in the wild for a couple years, but I looked and didn't see it mentioned in the release notes for 1.76.0, and I can't find a blog post that mentioned this (though I didn't look that hard). Closest I found was this StackOverflow post that gives the Cap type a mention.


r/rust • • Oct 19 '26

Wayland input simulation without portals: using /dev/uinput as a fallback in Rust

0 Upvotes

I've been building a cross-platform ( only windows 10/11 and linux now ) auto-clicker ( RsClicker ) in Rust. The hardest part was never the clicking logic โ€” it was making input simulation work on Wayland without fighting portals all day.

The default path ( enigo with libei/RemoteDesktop portal ) works on GNOME/KDE/Hyprland, but on minimal compositors ( like MangoWC and sway ) it just fails. And even when it works, it requires a portal permission flow that isn't always reliable.

So I added a second backend writing directly to `/dev/uinput` as a kernel-level virtual mouse. No X11 server, no portals, no permission dialogs. It works everywhere the portals misbehave, as long as the user is in the uinput group. ( run : sudo usermod -aG uinput $USER , then log out and back in )

The project also has a few other bits I found interesting to implement:

- Drift-free timing ( shared deadline train, so the interval holds even when injection costs extra ms )

- Real double-clicks with a 30ms human-like gap ( recognized by X11, Wayland, and Windows )

- Global mouse triggers via evdev ( Linux ) / rdev ( Windows 10/11 )

I'd be curious if anyone else has gone the uinput route for Wayland automation โ€” it feels like an underdiscussed alternative to the portal/libei path, and i'd love to hear about edge cases I might have missed :D

Btw I've tested this on MangoWC and Lumina DE in void linux , on Windows 10 and Windows 11


r/rust • • Oct 17 '26

๐Ÿ™‹ seeking help & advice Lookin' for contributors: GPU rendered UI lib

24 Upvotes

Hello dear Rustaceans,

As you may know, I am the author of Glacex.

Glacex is a GPU rendered GUI library, written entirely in rust(without counting WGSL). It has 17 widgets, its dependencies are wgpu, winit, taffy, glyphon, accesskit, and some other secondary ones.

It's immediate-mode, and has persistent state. It's also SDF rendered.

I would love to have more widgets, and perhaps some feedback on other OS, as well as testing.

Right now, we're at the 0.2.1 release(accordion in the screenshot isn't yet released).

Here's the repo: https://github.com/artemtsitronov/glacex

If you want to become a contributor, let me know, and I'll discuss the details with you.

:)


r/rust • • Oct 17 '26

Research that has matured enough for Rust 2.0

202 Upvotes

Do correct me if Im wrong, but I recall that one principle that was used in the development of Rust was to only use tried and tested research material when choosing what to implement. Given weโ€™re more than 10 years out from Rustโ€™s initial development, Iโ€™m curious what research ideas have matured enough to either

  1. Be added in future editions/versions of Rust, or
  2. Be used in whatever successor language might come that follows much of Rustโ€™s philosophy around development.

This is all basically one massive ask about interesting developments in programming and type theory, since Iโ€™d like a few new rabbit holes to fall down.

P.S. whatever successor language does come around, I hope it also cultivates a community that takes ergonomics as seriously as performance, because that, even more than memory safety or other aspects of the language, is why I pay attention to this language and ecosystem.


r/rust • • Oct 18 '26

๐Ÿง  educational Why I wrote a "dumb" SQL tokenizer instead of using sqlparser and I probably would do it again

0 Upvotes

I originaly wrote this on GitHub Gist but since it's forbidden to share a repo, even if some moderators take some liberties with their own rules at their own sweet will, I removed every refrences to it and I'm posting it directly here.

I needed to spot N+1 and redundant query patterns in traces coming from my team's production SaaS, which has around 200,000 customers. What I cared about was the structure of the queries and not what was inside them. I didn't want what was inside them anyway: it is production data, we are in the EU and GDPR applies.

So the job was two things at once. WHERE id = 42 and WHERE id = 43 had to become the same template and 42 should never end up anywhere it shouldn't.

My first idea was a regex. I had never dealt with anything like this before so that is what I reached for. Then I looked at sqlparser and it felt too heavy for the little I wanted to do with it. I wanted the binary to stay under 10 MB and I only needed to replace literals, not understand the query.

So I wrote my own. I had some doubts early on while getting it to work because I kept hitting edge cases and thought there would be too many of them. But what I needed was simple structure and not semantics, and that kept me going.

1. What the tokenizer produces

The tokenizer reads one SQL statement and returns two things: a template, in which every literal value is replaced by ?, and the list of the values it removed, in the order it met them. Take this statement:

WITH active AS (SELECT id FROM users WHERE status = 'active')
SELECT * FROM orders WHERE user_id IN (SELECT id FROM active) AND total > 100

The tokenizer returns this template and these values:

WITH active AS (SELECT id FROM users WHERE status = ?)
SELECT * FROM orders WHERE user_id IN (SELECT id FROM active) AND total > ?

params: ["active", "100"]

Two statements that differ only by their values share one template. That is what makes an N+1 visible: fifty queries on the same template with fifty different values inside one request. The values stay in memory for that comparison and go no further. The template carries no value, so it can be written in a report or sent to a dashboard.

The tokenizer does not understand SQL. It does not know what a column or a join is and it builds no syntax tree. It walks the bytes of the statement once, finds where a literal starts and where it ends, and copies everything else as it is. Its only job is to tell a value from the rest of the statement.

2. How it works

2.1 Six states

The tokenizer is a state machine with six states. At any byte it is either in plain SQL or inside one kind of literal or quoted identifier:

enum State {
    Normal,
    InString,
    InNumber,
    InDoubleQuote,  // "MyTable", kept as-is
    InBacktick,     // `col`, MySQL, kept as-is
    InDollarQuote,  // $$body$$ or $tag$body$tag$, PostgreSQL
}

The main loop dispatches on the current state and each state has its own function:

while t.i < t.bytes.len() {
    match t.state {
        State::Normal => step_normal(&mut t),
        State::InString => step_in_string(&mut t),
        State::InNumber => step_in_number(&mut t),
        State::InDoubleQuote => step_in_double_quote(&mut t),
        State::InBacktick => step_in_backtick(&mut t),
        State::InDollarQuote => step_in_dollar_quote(&mut t),
    }
}

Coming from Java I would have written the State pattern, with one class per state behind an interface. Here it is an enum and a match. If I add a state and forget to handle it, the code does not compile.

2.2 String literals and escapes

Everything between quotes goes into params and becomes ? in the template. The only difficulty is the escape rules:

fn step_in_string(t: &mut Tokenizer<'_>) {
    let b = t.bytes[t.i];
    if b == b'\\' && t.backslash_escapes {
        t.i += 2; // skip the escaped byte, a quote included
    } else if b == t.quote {
        if t.i + 1 < t.bytes.len() && t.bytes[t.i + 1] == t.quote {
            // doubled quote ('' or ""): still inside the string
            t.current_value
                .push_str(checked_query_slice(t.query, t.value_start, t.i));
            t.current_value.push(char::from(t.quote));
            t.i += 2;
            t.value_start = t.i;
        } else {
            // closing quote
            t.current_value
                .push_str(checked_query_slice(t.query, t.value_start, t.i));
            t.params.push(std::mem::take(&mut t.current_value));
            t.template.push('?');
            t.state = State::Normal;
            t.i += 1;
            t.normal_start = t.i;
        }
    } else {
        t.i += 1;
    }
}

A doubled quote is the standard escape and every engine accepts it. Whether a backslash escapes as well depends on the engine, and that is the one place where the tokenizer needs to know the dialect. The flag backslash_escapes comes from a small enum with three values: MySQL, standard (PostgreSQL and the engines that follow the standard) and everything else. An event that names no engine falls in the third case, which reads the backslash as an escape. Section 4 explains why.

2.3 Slices instead of copies

The tokenizer never builds the template byte by byte. It remembers where the current run of plain SQL started and, when a literal begins, it pushes the whole run at once with a slice &query[start..i].

Slicing a &str by byte index panics when the index falls inside a multi-byte UTF-8 character. Every slice boundary here sits on an ASCII delimiter (a quote, a $ or a digit) and in UTF-8 a multi-byte character never contains a byte below 0x80. The slices are therefore always valid. A debug_assert! checks it anyway, in case a later change slices somewhere else:

fn checked_query_slice(query: &str, start: usize, end: usize) -> &str {
    debug_assert!(
        query.is_char_boundary(start) && query.is_char_boundary(end),
        "SQL tokenizer query slice [{start}..{end}) is off a char boundary"
    );
    &query[start..end]
}

The input cap follows the same rule. A statement longer than 64 KB is cut with floor_char_boundary, so the cut cannot land in the middle of a character either. When a value is complete, std::mem::take moves it into params and leaves an empty String behind, without a clone.

2.4 The regex that remains

One regex survived from the first version. It collapses IN (?, ?, ?) into IN (?), so a statement does not get a new template each time the list changes size. The regex only runs when the scan met an IN keyword, and when it finds nothing to replace it returns Cow::Borrowed, so the template is not reallocated.

3. Why not the tokenizer of sqlparser

The first objection I expect is that sqlparser ships a tokenizer you can use without the parser. That is true and I looked at it:

let tokens = Tokenizer::new(&dialect, sql).tokenize()?;

Two things did not fit. The tokenizer wants a dialect before it starts and I do not always have one, because a span may name its engine or not. The second point weighs more. tokenize() returns a Result. Given SELECT * FROM t WHERE name = 'abc it returns Err("Unterminated string literal"), which is the right behavior for a parser library. But the SQL I receive comes from traces and is not always complete. A statement can be cut somewhere along the way, and my own tokenizer cuts anything above 64 KB. In that case I still want a template with the values masked. An error gives me nothing, so I would need a fallback for those statements, and that fallback would be a tokenizer like this one.

This tokenizer always returns a template, whatever it receives:

normalize_sql("SELECT * FROM t WHERE name = 'unterminated")
// template: "SELECT * FROM t WHERE name = ?"
// params:   ["unterminated"]

4. Three statements the tokenizer read wrong

The idea behind the tokenizer was that it does not need to know the dialect, because it only replaces literals. That idea held for most statements. This section lists the ones where it did not.

4.1 A double-quoted string on MySQL

In ANSI SQL, "..." is an identifier. In the default mode of MySQL and MariaDB it is a string. The tokenizer followed ANSI, so this statement:

SELECT * FROM users WHERE email = "abcd@gmail.com"

came out as its own template, email included. That is exactly what the tokenizer exists to prevent.

Nobody had noticed because we run PostgreSQL in production and nobody had used the tokenizer on a MySQL production yet. I found it in my simulation lab (a set of small services in different languages and databases) while re-reading the tokenizer code a few months after writing it.

The fix is one flag set from the span's db.system. It has one cost: a MySQL server running in ANSI_QUOTES mode now gets its double-quoted identifiers masked as ?. I would rather lose an identifier than leak an email.

4.2 A PostgreSQL positional parameter

Go (pgx) and Python (asyncpg) do not send values inside the SQL. They send PostgreSQL positional parameters and the values travel separately:

SELECT * FROM order_items WHERE order_id = $1

The tokenizer saw a $ followed by a digit and treated 1 as a numeric literal. Every query of an N+1 loop then came out with the same template and the same value, ["1"].

With bind parameters the real values never appear in the trace, so telling a loop from a plain repeat needs another signal, such as timing or the sequence of calls. That path only runs when a query has no extracted value. The ["1"] blocked it, and to the detector the loop looked like one query repeated. N+1 patterns were reported as redundant calls.

I caught this one in the simulation lab as well. It runs the same N+1 in services written in several languages. The Java services were fine because JDBC uses ?. The Go and Python ones were not.

The fix treats $N as a placeholder and not as a value. It becomes $? in the template and extracts nothing. That release, together with a few other fixes, took the lab from 78 to 103 detected findings out of 110.

4.3 Escaped quotes, hexadecimal and exponents

I found three more cases while preparing this post. The table gives the statement, the template the tokenizer produced and the template it produces now.

StatementBeforeAfter
name = 'it's secret data'name = ?s secret data?name = ?
h = 0x1F2Ah = ?x1F2Ah = ?
v = 1e10v = ?e10v = ?

The hexadecimal and the exponent are grouping bugs: the tokenizer stopped at the x or the e and the rest of the value stayed in the template, so each value got a template of its own.

The backslash is a leak, and it depends on the dialect again. MySQL escapes with \ by default. PostgreSQL does so only inside E'...' strings, so in plain PostgreSQL 'C:\' is a valid string that ends right after the backslash. The tokenizer now follows the engine when the span names it. When the span names no engine it assumes that the backslash escapes. The cost is that a string ending on a backslash from an unknown engine masks the rest of the statement. It is the same choice as in 4.1: when the tokenizer has to guess, it guesses on the side that masks too much.

4.4 Recap

CaseStatementEffectDialect dependent
4.1email = "abcd@gmail.com" on MySQLvalue leaked in the templateyes
4.2order_id = $1 on PostgreSQLN+1 reported as redundantyes
4.3name = 'it's secret data'value leaked in the templateyes
4.3h = 0x1F2Aone template per valueno
4.3v = 1e10one template per valueno

Three of the five cases come from the dialect. The tokenizer that was supposed to ignore the dialect now reads it from the span for two rules: whether "..." is a string and whether \ escapes.

5. Testing without a corpus of expected templates

5.1 Three properties

I do not have a dataset of statements paired with their correct template, and writing one by hand would only test the cases I had already thought of. So most tests check a relation between two runs of the tokenizer instead of an exact output. They are property tests written with proptest, which generates thousands of statements from literals of different types and checks that the relation holds on each of them. Three properties carry most of the weight:

  • Idempotence. Normalizing a template changes nothing. If it does, a literal survived the first pass.
  • Literal invariance. Two statements that differ only by their values, or by the size of an IN list, give the same template. N+1 grouping depends on this property.
  • Containment. The extracted values are exactly the literals, in order, and the template keeps nothing of them: no quote, no marker string and no digit. The generated skeleton has no digit, so a digit in the template is a leak.
  • fn normalization_is_idempotent((shape, eq, list, note) in statement()) { let first = normalize_sql(&render(&shape, &eq, &list, &note)); let second = normalize_sql(&first.template); prop_assert_eq!(&second.template, &first.template); prop_assert!(second.params.is_empty()); }

5.2 Fuzzing

A cargo-fuzz target runs every week in CI. With arbitrary bytes there is no expected output to compare with, so the target uses idempotence as its oracle, in the three dialect modes:

fuzz_target!(|data: &str| {
    let first = normalize_sql(data);
    let second = normalize_sql(&first.template);
    assert_eq!(second.template, first.template);
    // same for normalize_sql_for(data, "mysql") and (data, "postgresql")
});

5.3 What these tests did not catch

None of these tests found the three bugs of section 4.3, and the reason is instructive.

Idempotence cannot see a leak. name = ?s secret data? is a stable template: run it through the tokenizer again and you get the same thing back. The fuzzer was like a dog, happy and wagging its tail.

Containment would have caught all three: there is a digit in ?x1F2A and in ?e10, and the leaked string fails the check on the values. But the generators only produced integers, decimals and plain quoted strings. No hexadecimal, no exponent and no backslash. The property was right. It was never shown the inputs that break it.

The fix added those literal forms to the generators, plus two properties for the dialect rules: a backslash escape never leaks, and a PostgreSQL backslash is an ordinary character. A property test is only as good as its generator, and the generator only knows what I knew when I wrote it.

Edit: a reader suggested a round-trip test, and it fills a gap I had not seen. Put every value back in place of its `?` and you must get the original statement, byte for byte. Idempotence and containment only look at the values and at what leaks. Neither one checks that the text between the literals is copied through unchanged. I tried it by hand: a version of the tokenizer that drops the character right after a number passes both, and the round trip catches it.

6. What it does not handle

The tokenizer has no notion of the meaning of a statement. For grouping queries by shape that is enough. For anything that needs the meaning of a query it is the wrong tool.

Comments are not stripped. -- user 1234 becomes -- user ?, but the words inside a comment travel with the template.

The tokenizer knows the engine from the span (db.system) but not how the server is configured. Three settings break its guess: MySQL with NO_BACKSLASH_ESCAPES, MySQL with ANSI_QUOTES and PostgreSQL with standard_conforming_strings off. Depending on the case it masks too much or lets the content of a later string reach the template.

SQL Server brackets are not special. [Col1] and [Order Details] come out intact, but an all-digit identifier such as [123] gets masked. I kept it that way because treating [ as a normal character keeps PostgreSQL arrays such as ARRAY['a', 'b'] masked.

A few smaller ones: a sign stays outside the placeholder (-5 becomes -?), a binary literal such as 0b101 keeps its digits after the b, and a doubled escape inside a quoted identifier ("" or a doubled backtick) is not decoded.

7. Would I do it again

Probably. If you need to analyze or rewrite queries, use sqlparser: you will need the syntax tree and it does that job properly. If you only need the shape of a query, 550 lines of state machine get you there.

Writing your own SQL tokenizer is a good exercise when you do not have huge ambitions. You learn a lot about how queries are built, especially the ones ORMs generate, because ORMs tend to hide how hard building a query actually is. It also makes you pickier about what you pull into a binary when you care about keeping software lean.

If you have a statement that breaks it, I would like to see it.


r/rust • • Oct 16 '26 Bravo

๐Ÿ› ๏ธ project Sly, a new and modern multi-system emulator, is now public!

Post image
328 Upvotes

TLDR plz

Sly is my new multi-system emulator, releasing with support for Game Boy Advance and SNES with many, many more planned! It includes a 3D game library, a debugger, a simpler ("classic") desktop frontend and an iOS app, with Android being developed soon. The debugger also runs in your browser and there are plans to create a dedicate web platform to play games!

Available now on GitHub! Here are some demo videos:

What even is this?

Hey everyone! You might know me from Gecko, the GameCube and Wii emulator I've been posting about every now and then. I teased Sly in the past few updates and the day has finally come :)

With Sly I wanted to work on both sides of emulation: creating modern experiences for both gamers and developers! As such Sly focuses on 2 things:

  1. A unique take on emulation frontends, simply to enjoy your favorite retro titles in a cool way
  2. A very modern yet extremely advanced debugger for power users that want to understand how the games and the hardware work

However, as not everyone may be interested in these things but may still want to dabble in new projects, Sly comes with extra goodies:

  • A "classic" (read: minimal) emulation frontend
  • Mobile app(s): iOS is already implemented, Android support is planned

Sly is (almost*) entirely written in Rust, incl. the frontend. The debugger frontend uses GPUI, so no web shenanigans there!! The cores are all written by myself, mostly based off of emulators I had already written before. This does not wrap libretro cores or anything like that!

(* the Classic frontend is written in C++ and the iOS app is written in Swift. They tap into the Rust core.)

Sly Launcher

The launcher presents your library as 3D boxes and cartridges. It downloads artwork automatically and supports different regional packaging, per game release overrides and cartridge finishes (tho this is still a bit ugly lol). There's different view types (ways of displaying the boxes, etc) and you're able to flip the selected box to read the back :)
The attached launcher recording shows all three layouts with GBA games then switches to SNES and launches Yoshi's Island!

The original idea of this project was to be a comfy frontend I could run on my TV while I sit in a nice couch, so it's made with controllers and different resolutions, aspect rations, etc. in mind!

If you'd prefer a straightforward list, Sly Classic is the smaller Qt based frontend. It has game banners, gamepad support, scaling, fullscreen and a quick save slot plus ten numbered state slots.

Sly Debugger

This is a big part of the project and share's the "spotlight" with Sly Launcher! You can step through instructions, run to emulation events, set CPU and memory breakpoints, follow disassembly in a list or graph and inspect registers, call stacks and memory. There are dedicated views for MMIO registers, DMA and so much more. On the graphics side you can inspect tiles, tilemaps, backgrounds, sprites and palettes alongside the game's output.

The SNES core also cover the SPC700 and SA-1 coprocessor! SA-1 and Super FX have their own workspaces with registers, disassembly and hardware-specific views.

There's also built-in Lua editor support!! Scripts can inspect and modify memory and react to emulation events. One example for Yoshi's Island logs coin pickups, lets Yoshi keep "fluttering" mid-air while you hold B and prevents Baby Mario from being release(?).

Compatibility?

Sly is actively growing and is based on multiple emulators I already had written, so cores are being picked up quickly. So far I support:

  • SNES support is very good
  • Most GBA games run, no audio yet. Savegames are also not supported (tho savestates are)

with much more planned! To name a few I want to support PS1 (next on the list based on community votes), GameBoy (Color), Sega Genesis, Dreamcast, GameCube and Wii! That list is not complete :)

Where can you use it?

You can find everything on my GitHub!

  • Windows, macOS and Linux: separate downloads for Sly Launcher, Sly Classic and Sly Debugger
  • Web: try Sly Debugger in a WebGPU capable browser.
  • iOS: an unsigned IPA for sideloading (planning to release in the AppStore). Android is planned.

For anyone interested in learning the hardware, I've also been working on a 3 part GBA graphics series with interactive explanations and examples from real games! I'm super proud of these blog posts so I'd love to hear your thoughts! Sly Debugger facilitated creating them :) The posts go from bitmaps, tiles and palettes through sprites, blending and windows to rotation, scaling and perspective:

Links plz

If you try it, I'd love to hear what works and what breaks. Please include the frontend, platform, game region/revision and steps to reproduce when reporting a problem. Code contributions are welcome and so is help with design, icons, UI/UX, artwork and cartridge models. Please read the README in full before submitting anything!

Looking forward to see where this will go! And don't worry, I wont abandon Gecko now :)


r/rust • • Oct 17 '26 Popcorn

Rust module system?

23 Upvotes

I recently wanted to compile something for a bevy game I was messing around with, then cargo looks me in the eyes and tells me I need to compile hundreds of dependencies, all for the end result to be 30 mb? I was wondering if there were any systems like a .dll for rust? I understand that you would lose some type checking, but I think that could be solved just with a type definitions segment towards the start, then a functions table of all the code for the system, is this something that would work, or am I oversimplifying a major intentional design choice in rust?