11 ms·
How I Start: Rust
- saagarjha 7y ago> Adding the --vcs git flag also has cargo create a gitignore file so that the target folder isn’t tracked by git. Doesn’t cargo make a git repository by default, anyways? Why doesn’t it add a sensible gitignore in the process?
- bcheung 7y agoWhy "cargo init" instead of "cargo new"? Also, wondering when Rocket will be usable without nightly. That makes me concerned that it is not stable enough.
- status_quo69 7y agoI've used it for 6 months on the async branch and the master branch and master was incredibly stable. Async is less so because it's a large scale effort to try and convert over the entire API with minimal breakage(apart from fairings/middlewares). You can pin to a specific stable nightly if you're needing extra sanity, although CI makes this hard with the nightly docker images. There's a tracking issue for proc_macro_hygine here: https://github.com/rust-lang/rust/issues/54727 https://github.com/rust-lang/rust/issues/54727 Looks like the last feature is almost stable!
- xena 7y agoI use `cargo init` because I'm used to creating project folders myself.
- nicoburns 7y agoAsync and stable support are both coming soon. I'd estimate within 3-6 months.
- steveklabnik 7y agoRocket uses three nightly features: * proc_macro_hygiene - this feature lets you call procedural macros in more places than you can today. https://github.com/rust-lang/rust/issues/54727#issuecomment-580647446 https://github.com/rust-lang/rust/issues/54727#issuecomment-... might be a bit too in-the-weeds, but the TL;DR is that there seems to be a path forward for stabilizing what Rocket needs in a near-ish timeframe. * proc_macro_diagnostics and proc_macro_span: these purely give Rocket better error messages in some situations. With the first thing stabilized, this could be dropped to get things compiling fully on stable, though diagnostics would regress from nightly. In general, these last bits of things Rocket needs aren't changing a whole lot.
- xena 7y agoAuthor of the post here, feel free to ask me anything!
- artursapek 7y agoDo you write code in that color scheme? (Dark green everything on a black background)
- xena 7y agoNo, I'm not sure why the gruvbox theme I use has that for code examples. Here's what my coding view looks like: https://i.imgur.com/oWFlmgB.png https://i.imgur.com/oWFlmgB.png
- seaish 7y agoWhy no `cargo run`?
- xena 7y agoI'm used to doing stuff with WebAssembly, that is beyond the reach of `cargo run` most of the time.
- CryZe 7y agoActually even for wasm you can make `cargo run` work. You just need to write a script for it and use it as the target runner. Especially for WASI this is great where you can just specify wasmtime as the runner and then cargo test / bench / run all work perfectly fine.
- hamaluik 7y agoI was actually just searching for this the other day but couldn’t quite figure it out. Do you have any guides on doing just this? Thanks!
- hrombach 7y ago
- anurag 7y agoIn case it helps, Render (https://render.com https://render.com) has first class support for Rust webapp hosting without needing Docker. And here's the Hello World URL for this tutorial! https://rust-nix-helloworld.onrender.com/hostinfo https://rust-nix-helloworld.onrender.com/hostinfo More info at https://render.com/docs/deploy-rocket-rust https://render.com/docs/deploy-rocket-rust. I'm the founder; happy to answer questions here or in our user chat at https://render.com/chat https://render.com/chat.
- rafaelgoncalves 7y agoDidn't know about this render.com, very nice hosting. Thanks for this.
- status_quo69 7y agoOne thing I wish was covered in these is how to go past 'hello world', especially things like error responses and serialization models. It took me a while before I got to the point where I think I can be productive in both querying the data from the front-end and getting back the shape that I wanted. Couple of lessons learned: - Start with separating your API models and DB models out early, you'll be thankful later. You can also help yourself out by implementing From/Into for your database models to your API models. Obvious lesson from CQRS but since rust doesn't allow easily for ad-hoc structs this is a must, especially since there are a lot of examples out there that put a Serialize/Deserialize onto the database object. - If you have deeply nested objects, look into JSON:API or Juniper. There are a couple of crates that implement the jsonapi spec and I'm hacking on one of them to make it a bit more usable for my personal pet project, but Juniper will make your life easier especially if you're starting with a React frontend. - Understand that there's still a tracking issue for a few items such as multipart data, although there's a third party crate that integrates the multipart crate into rocket. However if you need an upload you could probably get away with just returning a signed S3 endpoint to the user, which is what I ended up doing - If integrating with redis or some other database that's not application critical, there is no (as far as I'm aware) graceful degredation that comes out of the box with rocket_contrib, unless there was some config value I was missing. This is usually fine, since you can easily declare an ad-hoc fairing that connects to redis and falls back to an Option::None in the case where it couldn't connect. And finally, if you want to support different error status codes yourself based off of mappings from internal errors (such as 409 conflict when inserting the same data), your best bet is an enum that's wrapped by a responder derive like so: #[derive(Responder, Debug)] pub enum UserApiError { #[response(status = 500, content_type = "json")] Unavailable(Json<UserApiErrorMessage>), } impl UserApiError { fn wrap_body(message: &str) -> Json<UserApiErrorMessage> { Json(UserApiErrorMessage { status: "error".to_string(), message: message.to_string(), }) } pub fn unavailable(message: &str) -> Self { Self::Unavailable(Self::wrap_body(message)) } } Which can be called like: Err(UserApiError::unavailable("The database is gone!")) Most of my other lessons learned come from diesel, both are an absolute pleasure to work with and have given me incredible confidence while coding.
- 7y ago
- rvz 7y agoAs much as I use Rust personally and for some of my own pet projects, for serious HTTP services and introducing it into a new developer team, I'm afraid that isn't feasible for my requirements. I'm put off with the immaturity of the crates ecosystem and the costs it will bring. Most of the crates are not even 1.0 including Rocket and that also requires a nightly build which isn't good enough for my requirements. (I only use stable Rust). As a language it is mature, but the crates ecosystem is of lesser quality, especially for applications like HTTP servers. I've already overcome its learning curve, but the devs who swear by other languages that pay them well may not be so forgiving.
- timClicks 7y agoOne thing I've always wondered about this "not even 1.0" mentality is, why is a version number so significant? Sure it's an easy proxy, but it's not perfect. That number could be arbitrarily changed tomorrow and the maturity of the project hasn't necessarily changed.
- setr 7y agoMost versions don't mean much on their face -- every project has a different definition and way of versioning (usually undefined and fairly arbitrary, or based on client feature requirement and need-to-deploy). But 1.0 is special -- pretty much everyone agrees on its meaning: it's ready for others to use it, and I'm willing to stand by it. Sub-1.0 is also special, and generally agreed upon -- no one should consider this production-ready, and I'm not ready to defend it.
- tracker1 7y agoTo me sub-1.0 means you can't rely on even point releases being non-breaking changes, more than the suitability for production use... though that difference sees some overlap. Using node as much as I have, I've gotten used to updating dependencies fairly often and spending maybe a day a month on keeping things updated and smooth. I also tend to dig into nested dependencies and my bundle sizes (more on the front end).
- 7y ago
- deepsun 7y ago> routes!(...) > routes_with_openapi!(...) Macros, macros everywhere. I understand their use in basic functions like println!(), but I'm worried too much macros in client code will lead to problems (unreadable, clashes that will need custom names). If there are no problems with macros, why not making every function a macro then?
- steveklabnik 7y agoRocket takes the position (or at least, this is what I understand, maybe it's changed) that developer ergonomics are the most important thing. Macros can significantly clean up boilerplate, and so Rocket tends to use a lot of them. There are other options if you disagree, but this approach has gained it quite a following.
- AgentME 7y agoMacros let you do work at compile-time like inspect the types of their parameters. A macro can do things like automatically generating code that converts function's return type into a JSON string. If you need that, use a macro, but otherwise, you can and probably should use a function.
- dgb23 7y agoMacros are extremely important to make rust productive. It sometimes feels as if you were writing in a higher level language. One of the best examples is the serde crate. What a beautiful library!
- kibwen 7y agoMacros are powerful, but they should be used sparingly because they often have outsize effects on compilation times and degrading error message quality. I personally give myself a "macro budget" where macro-based solutions are prioritized based on benefit to a particular project.
- pjmlp 7y agoWith great power comes great responsibility. Based on my experience with macros, across several languages with support for them, macros are beautiful until you need to debug them under time pressure on a critical customer issue on a code base that you never seen before.
- CoffeeDregs 7y agoLove seeing more Rust. It's good stuff. extern crate rocket; I thought "extern crate XYZ" was old-style? "use XYZ"?
- cjbassi 7y agoIt's still fairly common to use in conjunction with #[macro_use] for when you want to easily import all of the macros from a crate across your entire project. But yes, you could do use rocket::{get, routes}; instead.
- jakearmitage 7y agoI love the way this tutorial was written. I can't explain what exactly, but I found the entire experience to be very didactic. Congrats, and thank you! Wish we had more of this.
- MuffinFlavored 7y ago> Rocket has support for unit testing built in Do I really want my HTTP service framework to be my unit testing framework too? Could these not be separated?
- hathawsh 7y agoI believe the author meant that Rocket provides helpers to make unit testing easy. The tests are written using the ordinary "#[test]" Rust annotation and run by the standard Rust test runner.
- baby 7y agoA few comments: * missing `use rocket_contrib::json::Json` * `unwrap_or` is better than `or` followed by `unwrap` * the psutil crate already has hostname info * the psutil crate doesn't provide an uptime function if you're on a unix system. * once you setup the hostinfo route, your example says to curl the index * your docker link is broken