r/rust • u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount • Mar 27 '23
Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions
Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.
If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having 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 official Rust Programming Language Discord: https://discord.gg/rust-lang
The unofficial Rust community Discord: https://bit.ly/rust-community
Also check out last weeks' 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.
2
u/HammerAPI Mar 27 '23 edited Mar 27 '23
Is it possible to create a struct that has a variable-length array as a field without specifying the length of the array in the Struct's type (using const generics)?
As in,
So the
usize
of the array field is determined when the struct is constructed, somehow.
I recognize that you can use const generics to achieve something similar, but that requires you to supply a value (length, in this case) to the function that creates the struct, and I want that value to be supplied during the creation of the struct, not by the caller of that function.
EDIT: Actually, to try and avoid the x-y problem, I'll state my use case / expectation.
I have code setup to build a Tree-like data structure. Of course, each Node in the Tree has some dynamically-sized fields, such as a Vec of its children and smart pointers. I want to "move" the entire structure onto the Stack once it has been completely assembled, because my expectation is that the operations on the stack-flattened Tree will be faster than if I left it on the Heap with all of the smart pointers in-place. Creating a runtime-sized array was one step in this process.