4 ms·
Not GP, but for me it was having to think hard about what it means to actually ’own’ data, and learning to structure programs appropriately.
by KineticLensman 3y ago
Not GP, but for me it was having to think hard about what it means to actually ’own’ data, and learning to structure programs appropriately.
- junon 3y agoThis. If rust is giving you a hard time, you're probably designing something some subpar way. That's been my biggest takeaway switching to rust.
- PartiallyTyped 3y agoI’ll disagree a bit here. There are situations where absence of (more) dynamic typing can make your life harder than it already is. An example of this is generating a JSON and only learning the correct schema at runtime. Or connecting to an external database with tables of unknown structures that you can’t type check. You can, of course deal with all these by wrapping the objects in some structure; but it’s just one more thing you need to keep in mind.
- Permik 3y agoNothing in rust actually limits or makes it any harder to handle something "more dynamic" like JSON. In rust you just make a JsonValue enum that represents the values and the data types that you're handling. In something like Python, you just use the built-in runtime value type to represent the structure. Either way you still have to plumb up the data ingestation and your codepaths that you want to use the dynamic data in. For example you still have to check if the "name" key's value is a string and not an object, array etc. Otherwise, you probably could just use some accessors that access the values willy nilly that coerce them to text, return an error or panic, that could also exist in rust (some of these things you would have to wire up manually, some are provided by the language and others are fixed by just annotating structs.) To summarize, fail to see the point you're trying to make, do you have some specific example you are thinking of that I can take a look at?
- PartiallyTyped 3y agoYou made my point, in that it is a slightly more involved process over say Python. I didn't say that it's a "problem" or "impossible", just that it is making your life slightly harder. Something that I am interested in doing is generating a json schema at runtime, and then parsing some incoming json to see if that matches the target schema. Do you know of any crate already providing this kind of functionality?
- Permik 3y agoWould something like Valico fit the bill? https://crates.io/crates/valico https://crates.io/crates/valico When looking for crates lib.rs is an excellent search utility for searching crates that you would need. For example: https://lib.rs/search?q=json https://lib.rs/search?q=json
- PartiallyTyped 3y agoTIL lib.rs exists :D I think it fits the bill. I was doing this on python side and aimed to use Pydantic and this looks close enough.
- gaganyaan 3y agoI haven't found dynamism like that to be a problem. It's slightly more verbose, but not by much. Instead of `foo: int = unknown['foo']` like in Python, you do `let foo = unknown.get_u64('foo')?` (or something like that) in Rust with `serde_json`.
- PartiallyTyped 3y agoI didn't claim that it's a problem, just making your life slightly harder and needing to enumerate all enum cases in e.g. serde_json.
- junon 3y agoYou don't, though? I'm not sure what kind of code you're referring to. Can you give an example?
- LawnGnome 3y agoSame. I gradually learned Rust while I was working in a job where I wrote C on a day-to-day basis, and after a while I realised that Rust was making me a better C developer by forcing me to think more systematically about ownership and lifetimes than I really had to that point. It significantly changed how I designed and structured my code, even in other languages. Not to say I couldn't have gotten the same education from another language, but something about Rust clicked for me that others didn't.