|
my work gives learning stipends and i want to learn rust (i think). is there any definitive guides or are the usual recommendations (udemy, pluralsight, etc) similar? or are these docs just what i should be focusing on? https://doc.rust-lang.org/1.0.0-alpha/book/README.html e: sorry i should also add javascript and python are my strongest languages, so i'm not coming in fresh
|
# ¿ Feb 4, 2023 01:00 |
|
|
# ¿ May 12, 2024 16:43 |
|
hello. i decided to learn Rust this week and to get my hands dirty with it, I decided to build an authorization API that I previously built in Python. It's going well so far What I've got working is: a fully dockerized rust HTTP server (using Actix), Postgres, and Nginx I've got the app talking to the database and have written a few initial migrations to get the DB set up and have those run on app start a bunch of endpoints returning dummy data like POST /register, POST /login, GET /profile what I am struggling big time with is the actual rust code and was hoping someone could help me out. I know i should probably just take a pause and spend a couple days watching some more Youtube, but I'm really trying to learn by fire here. So basically what I'm trying to do is return multiple errors to the user, based on the request body they send to me. So this for example: JSON code:
JSON code:
Actix exposes the request JSON in either Bytes or actual JSON like so Rust code:
Rust code:
Rust code:
|
# ¿ Sep 26, 2023 18:14 |
|
okay after some battling, here's what I came up with the endpoint: Rust code:
Rust code:
|
# ¿ Sep 26, 2023 20:42 |
|
gonadic io posted:Certainly looks functional. Some advice to improve it: thanks for the help! this was the one that was specifically tripping me up though because from_slice has no .get() method. Is there a way to check for the existance of a key without panicking that I was just not finding in my google searches? as far as I can tell, I can only do something like Rust code:
teen phone cutie fucked around with this message at 22:12 on Sep 26, 2023 |
# ¿ Sep 26, 2023 22:07 |
|
gonadic io posted:Look at the type signatures of the from_str and from_slice methods. Both can parse into anything that implements the Deserialize trait. If you then click on that and scroll down, you'll see that hashmap is one of those things. oh huh i told the method to return a hashmap and.....it just worked. This seems like magic. well thank you again. do you know of any docs that explains this kind of type signature conversion to me like i am a child? Python and Typescript are my only background, and some of this stuff is really hard to wrap my head around. Or rather, is this just something special serde_json is doing that isn't typical? e: we're in business now teen phone cutie fucked around with this message at 22:45 on Sep 26, 2023 |
# ¿ Sep 26, 2023 22:38 |
|
|
# ¿ May 12, 2024 16:43 |
|
thanks so much for the direction on this really! I joined 2 different discords this week asking these questions and got a bunch of answers ranging from "I would just have all the validation live in the client" or "i wouldn't aggregate errors at all and just return a single 'form invalid' error" and the best one, "just use a library for this" and other completely useless feedback that isn't teaching me how to use rust
|
# ¿ Sep 27, 2023 01:37 |