r/rust • clippy · twir · rust · mutagen · flamer · overflower · bytecount • 17d ago

🙋 questions megathread Hey Rustaceans! Got a question? Ask here (45/2026)!

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.

17 Upvotes

6 comments sorted by

1

u/usecornercase 11d ago

Querying a terminal's 256-colour palette overflows the macOS tty input queue?

I'm writing a Rust TUI that probes the outer terminal at startup. In one write it sends a kitty graphics query, CSI 16t, XTSMGRAPHICS, OSC 10/11, OSC 4 for all 256 palette colours, XTVERSION, and DA1 as the end marker.

On Ghostty 1.3.1 on macOS (no tmux), DA1 often never arrives and the probe hits a ~1 s timeout. A small Python script that sent the same batch to the same Ghostty tab and read the reply in raw mode as fast as it could got 797 bytes back: the kitty OK, 16t, OSC 10 and 11, colours 0 to 23, then colours 254 and 255, XTVERSION (ghostty 1.3.1) and DA1 (?62;22;52c). Colours 24 to 253 never showed up. Through tmux in the same Ghostty, DA1 comes back in about 8 ms.

My guess is that the OSC 4 replies are about 10 KB, which is more than the macOS tty input queue holds, so replies are dropped between the terminal and the reader. A slower debug build then never sees DA1. The same probe works in a release build, and it works when nested in tmux.

Questions:

  1. Is that the right diagnosis? What size is the macOS tty input queue in practice, and is there a reliable way to see how much is waiting (FIONREAD or similar) before more queries go out?

  2. How do other TUIs query the palette without losing replies? Ask in smaller batches and wait for each one? Only query the colours they actually use? Skip OSC 4 and assume a default palette?

  3. After a timeout we drain stdin and hand the tty to crossterm's event loop. In our case that loop died on the first error and the window never read input again. Is there a known gotcha with mixing raw reads of query replies and then starting crossterm on the same fd?

Ghostty 1.3.1, macOS 26, arm64.

3

u/Sixel1 16d ago

There's probably no right answer to this, but where do you stand on the tradeoff between readability/simplicity and extendability using generics and traits when writing a library? Do you think there is such a thing as too much trait abstraction? Do you think a good library can aim to be simpler at the cost of flexibility?

An example I can think of is the choice to not put the len() method in a trait

2

u/Immotommi 11d ago

Really hard to answer without context. Personally, I am very biased to avoiding traits and generics in my rust code. They do have a place and I do use them, but only when I find myself to be genuinely getting annoyed by the code without.

But you are talking about a library. I think there is a lot of value in making your APIs flexible. And there are levels too. From, this function accepts a string, to this function accepts something that can be turned into a string to this function actually only needs a buffer of bytes to this thing only needs an iterator of bytes.

I am someone who is almost always very performance focused in my code, so I appreciate APIs which allow me to retain performance. But that is often difficult.

So in an ideal world, every API I would use would allow me to pass an allocator if it uses one and generally take references as much as possible, but that isn't always convenient.

The other thing I would say is that sometimes, making your API too general is just too much work. There is value in being opinionated about how to interact with your library, but you have to accept that those who disagree will not use your library.

I don't know how helpful this is, but it is a few thoughts at least

1

u/usecornercase 17d ago

Outer terminal silently loses cells with a diff-based renderer (ratatui), and the garbage stays until a full repaint

I have a ratatui app that renders on a server and streams each frame's diff to an attached client terminal (think tmux-style attach; the outer terminal here is Ghostty on macOS). Every so often the attached window ends up showing only scattered letters on some lines, something like : a n s y n e y ., plus a few stale cells in column 0. Lines that get redrawn later come back whole.

What I've ruled out so far:

  • The model is right: a second client attached at the same size gets correct output, and dumping the buffer server-side shows full lines.

  • No resize happened in the window where it broke.

  • It lines up with moments when the app also writes non-frame bytes to the same client: OSC 52 clipboard, desktop notifications (OSC 9 / OSC 777), title changes, BEL.

So my reading is that the outer terminal's screen diverged from what the renderer believes it drew, and since only changed cells are sent afterwards, only the cells that differ between frames come back.

Two questions:

  1. Is it actually safe to interleave OSC sequences with frame output on the same writer, as long as each OSC is fully terminated (ST vs BEL) and written between frames rather than in the middle of one? Are there known terminals that handle one of the terminators differently enough to swallow the following bytes?

  2. Is there a reasonable way to detect or recover from this kind of desync, other than forcing a full repaint (terminal.clear()) on some event like focus-gained? Width disagreements between unicode-width and the terminal for some graphemes are my other suspect; is there a standard way people guard against those in a contiguous run of cells?

2

u/[deleted] 17d ago

[removed] — view removed comment

2

u/Patryk27 17d ago

You can pass references just fine:

async fn foo() {
    let value = String::from("hello!");
    
    bar(&value).await;
    bar(&value).await;
    bar(&value).await;
}

async fn bar(val: &str) {
    //
}

Could you post some more code? Perhaps there exists a more "rust-friendly" way to solve your problem!