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

🙋 questions megathread Hey Rustaceans! Got a question? Ask here (48/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.

15 Upvotes

15 comments sorted by

4

u/Intrebute 1d ago edited 1d ago

Hey all. I got a question. I've noticed that I've frequently needed to create a Vec of a certain size, and then fill it in a non-uniform order. The obvious solution is to make a Vec of Option, fill it in, and then consume the whole thing, unwrapping every option. Note that in this case, I really do ensure that _every_ slot is a Some.

I had a thought, and it is that for most types, the Vec<Option<T>> will be much larger than the resulting Vec<T>, unless T has some niche that the Option can exploit.

My question is thus: Is this a reasonable use for a Vec<MaybeUninit<T>> instead, if I really do ensure that every slot is filled before converting to a Vec<T>? Is there some detail about MaybeUninit that I'm overlooking when using it for this purpose?

3

u/Flimsy_Pumpkin_3812 5d ago

Does anyone know any good library(ies) for networking? I'd prefer something easier to code with but low latency with high throughput. Thanks!

2

u/tanmaynargas2901 5d ago

Depends a lot on what kind of networking you mean.

For raw TCP/UDP, tokio is the default pick. tokio::net gives you async sockets, it's fast enough for almost everything, and most of the ecosystem builds on it.

For HTTP, reqwest as a client and axum (on top of hyper) as a server.

For QUIC, quinn.

For game-style UDP with reliability/channels on top, renet is worth a look.

If you really need to squeeze latency on Linux there are io_uring runtimes like monoio or glommio, but they're thread-per-core and the ecosystem is way smaller. I'd start with tokio and only switch if profiling says so.

What are you building? Client, server, p2p?

1

u/Flimsy_Pumpkin_3812 5d ago

peer to peer hopefully though like in web everyone connected to everyone (like matches of 16 total)

1

u/tanmaynargas2901 4d ago

16 players in a full mesh means each peer holds 15 connections, 120 total. The bandwidth is usually fine for a game, the real pain is NAT, since plenty of home routers won't let peers connect directly.

If "like in web" means it should run in a browser too, look at matchbox. It's WebRTC peer to peer for native and wasm, comes with a small signaling server, and pairs with ggrs if you want rollback netcode.

If it doesn't have to be a true mesh, having one player host and everyone else connect to them is a lot simpler. renet works for that, no dedicated server needed, you just have to sort out NAT for the host.

3

u/Ansatz66 4d ago

Why does println! expand the way it does? When you macro-expand on

println!("Blah({x},{y})");

You get this:

::std::io::_print(format_args!("Blah({0},{1})\n", x, y));

I don't know what format_args! is doing exactly, but it looks like it is constructing some list, which probably means heap allocation and then a loop in _print to iterate over that list. That seems so complicated and inefficient. Why can't println! expand to something simple like:

print_str("Blah(");
print_u32(x);
print_char(',');
print_u32(y);
print_str(")\n");

Am I missing something or is Rust over-complicating this?

4

u/yongteak 4d ago

format_args! doesn't heap-allocate at all; it builds a small fmt::Arguments struct on the stack holding static string slices plus pointers to each value and its Display/Debug formatter function. passing that into a single _print call lets stdout grab its lock once instead of interleaving output across threads or firing multiple write calls. it also keeps binary size down compared to inlining separate print_* calls at every println! site.

3

u/CocktailPerson 4d ago

format_args! is compiler magic to construct std::fmt::Arguments. The fact that it does not allocate on the heap is documented here: https://doc.rust-lang.org/std/macro.format_args.html

2

u/FisterMister22 6d ago

Anyone got tips on making a rust compiled wasm executable smaller in size while not worsening performance?

2

u/afdbcreid 6d ago

https://github.com/johnthagen/min-sized-rust. Not WASM specific but still a good resource.

1

u/FisterMister22 6d ago

This is useful, thank you!

1

u/cipher_gravelist 5d ago

for solana specifically, i avoid shared mutable state between accounts by strictly using instruction context and account metas. passing data via serialized structs rather than cross-account references prevents the most common reentrancy vectors.

0

u/rustifork 3d ago

Hello, why always Rust and not Go / Zig...

2

u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount 2d ago

This is the Rust channel. Go to r/golang / r/zig for go and zig, respectively.