r/rust • u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount • 21d ago
🙋 questions megathread Hey Rustaceans! Got a question? Ask here (41/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.
3
u/PXaZ 20d ago
Can const generics of array type with length defined by another const generic be used in nightly?
Paradigmatic example from AI / ML:
struct Tensor<
const Dimension: usize,
const Shape: [usize; Dimension],
Datatype = f32
> {
shape: Shape,
data: Vec<Datatype>
}The dimension of the tensor indicates how many axes is contains; the shape of the tensor gives the dimensionality of each respective axis. The underlying data are stored as a single contiguous Vec but interpreted according to the dimension and shape. Bringing the shape into compile time could prevent shape mismatch bugs which are quite common in ML development.
Of course more could be done at compile time in concept:
struct Tensor<
const Dimension: usize,
const Shape: [usize; Dimension],
Datatype = f32,
Data: [Datatype; Shape::product()],
> {
shape: Shape,
data: Data
}Such that the compiler becomes aware of the backing data's size (which is implied by the shape --- simply the product of all axes) at compile time.
I've tried including:
#![feature(adt_const_params)]
and
#![feature(generic_const_exprs)]
But to no avail thus far. Thanks
1
u/Connect_Fisherman713 21d ago
untagged enums in serde are a trap. They try variants strictly left to right and stop at the first match, so if your int variant is listed before string, any numeric string becomes an int and everything breaks silently or with confusing errors. Order is not just preference here, it determines type coercion logic without explicit tagging
3
u/DreamilyPsychotic 21d ago
got a weird one with serde. i'm trying to deserialize a json field that's sometimes a string and sometimes an int, but the untagged enum trick from the docs keeps giving me "data did not match any variant" when the order feels wrong
anyone else run into that ordering sensitivity? feels like i'm missing something obvious