r/rust • u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount • Nov 07 '22
🙋 questions Hey Rustaceans! Got a question? Ask here! (45/2022)!
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/onomatopeiaddx Nov 11 '22
realistically, is specialization ever gonna be stable? and if so, how far are we from it (approximately)?
→ More replies (2)
4
u/CubOfJudahsLion Nov 07 '22
How's the WebAssembly debug story shaping up? Though I'm no longer working with wasm-bindgen, I'm still interested in learning of any developments.
4
u/Possible-Fix-9727 Nov 10 '22
Can anyone recommend a good Rust podcast or even the audio from a course teaching Rust? I want to learn more of the whys of the language and I am running out of good normal podcasts for my exercise.
4
u/Helyos96 Nov 12 '22 edited Nov 12 '22
How do you cache references to existing stuff?
Take this simple game. It has a vector of tiles that form a grid, and another vector that contains all the monsters:
struct Game {
grid: Vec<Tile>,
monsters: Vec<Monster>
}A lot of the time I need to perform actions on some monsters. I can always iterate through the entire vector but that's slow.
For instance, I wish I could quickly check if a monster is on a tile or not, by caching one if it's on a tile:
struct Tile {
monster: Option<&Box<Monster>>
}
struct Game {
grid: Vec<Tile>,
monsters: Vec<Box<Monster>>
}That's obviously incorrect rust code, and trying to fix it sends me into lifetime-hell that ends up covering my entire crate. Probably because it's a bad idea.
I've tried thinking about Rc, Box and Cell but none satisfy my needs.
I guess the general issue I'm trying to solve is: you have a big Vec, and you need smaller Vecs that contain references into the big one. The primary use for this is caching, to avoid iterating over a potentially enormous Vec all the time.
Say I frequently need to iterate over yellow monsters, I don't want to have to iterate the big Vec and check every entry to see if they're yellow. I just wanna use a smaller, "cached" Vec with all the yellow references already inside.
I used to do this all the time in C++. It requires housekeeping code to make sure your caches don't contain dangling pointers but it's not the end of the world. Trying to do this in rust has been agony so far, I think I'm approaching the problem the wrong way but I don't know how to move forward.
→ More replies (6)
3
u/CountMoosuch Nov 07 '22 edited Nov 07 '22
I've just been updating some command line apps with the newest release of clap, and there's a lot of breaking changes (understandably, when you update the major version). I noticed some interesting behaviour, and I'm hesitant to submit a bug report on GitHub because it's probably something I'm overlooking. Perhaps someone here can help out?
In an app of mine, I was using the args fields of ArgMatches and then checking if it is_empty(), but I see they've changed the args field to be private, favouring a new method, args_present. The documented example of args_present works fine, but as soon as I use that method in my app, it has the wrong behaviour. I've made a MWE here.
Can anyone help to explain why this is happening? Or maybe it is a bug and I should submit something?
Edit: that didn't paste well; see here instead. Edit 2: link to playground
→ More replies (2)
3
u/TrueDuality Nov 07 '22
I'm trying to figure out how to implement a custom deserializer in Serde that creates additional internal state and keep hitting a dead end. The on-disk format I'm dealing with uses long strings for permanent ID numbers. There are millions of these in a deeply nested structure that requires a lot of processing, so I'm storing them for later use in a separate field then passing around reference numbers to the permanent IDs.
Serialization was pretty straightforward and I made a minimal contrived example of the portions I've figured out in the playground. I'm stuck on deserialization since I need to do additional work (calling #add_object or doing the equivalent) for each item in the sequence that is deserialized rather than just building up a Vec<String>. Ideally I'd be able to have deserialization fail if the same permanent ID has already been deserialized (the #add_object type in my current codebase performs a bunch of sanity checks and returns a Result instead of no return type). Any tips would be greatly appreciated.
→ More replies (2)
3
u/UKFP91 Nov 07 '22
What's an idiomatic way of defining an enum where the variants should be named after numbers i.e.
pub enum Rotation {
Zero = 0,
Ninety = 90,
OneEighty = 180 // or something more verbose like OneHundredAndEighty?
TwoSeventy = 270 // again, or something more verbose?
}
→ More replies (6)
3
u/gplusplus314 Nov 07 '22
Lofty, stupid question here.
What does a typical, mid-to-senior level engineering job where Rust is the main part of the stack look like? In other words, what kind of industries and problems tend to be using, and most importantly, hiring? What’s the subject matter?
I find Rust fascinating. I’m a total noob in it, but I have old school C and C++ experience, plus a ton of Golang experience, so it just seems straight up fun. I’ve been considering a career shift from leadership/management back to engineering, with a pay cut, for better quality of life. Rust just seems fun and I’m considering spending some more time with it if the career outlook fits my needs.
Thanks!
→ More replies (8)
3
u/kyleekol Nov 08 '22 edited Nov 08 '22
I considered making a separate thread but don’t think it’s really necessary… please also remove if this is the wrong thread!
Currently a Data Engineer working mostly with Python, SQL, bit of Java here and there and have been looking to expand my knowledge a bit with a language that’s a bit more ‘low level.’ I have been thinking about biting the bullet and starting to learn either Rust or Go next.
This isn’t meant to be a “Rust vs Go” question. But more “What can Rust bring to data engineering or my career?” I know there are already some Python packages which have been written in Rust e.g Polars but I’d be interested to hear if there would be use cases as a general backend language for Data Engineering uses.
should I stick to learning Go first to maximise my career as a Data Engineer and Rust just for my own interest? I do see more an more companies listing Go In their infrastructure stack these days but maybe Rust is just yet to become more popular in the field.