Hacker News new | ask | show | jobs
by huyage 1405 days ago
I'm building an HTTP CRUD app on top of PostgreSQL. I have previously used Go for various CRUD apps. The experience is better except for compile time. My favorites:

1. Much more expressive type system: no more `interface{}`.

2. Algebraic data type with `enum`. Exhaustive check.

3. `serde` crate is lightyears ahead of Go's `json:"wtf"`.

4. Compile-time checks against DB with `sqlx` crate.

5. Logging is a breeze with `tracing::instrument`.

6. Using macros to reduce boilerplate has better UX than Go codegen.

What these give me is more confidence in development/refactoring because the compiler can guide me most of the way.

Low lights:

1. Build time.

2. Async is still clunky. e.g. Try to implement an `actix-web` extractor or middleware.

3. Please just stablize nightly rustfmt features already.

2 comments

I'm looking to do a bit of CRUD on top of PostgreSQL as well. Currently mid-way through learning the language.

Would you mind sharing some of your experiences? Do you find the 'stubbornness' of the compiler frustrating at all? How do you find the tooling?

Also, I've considered Diesel for ORM, so was wondering if you've been using that too.

> Do you find the 'stubbornness' of the compiler frustrating at all?

I actually love it. The more work I can offload to compiler the better. One simple example that frustrated me in Go was adding a field to a struct. You add the field the the whole thing still compiles even though the zero-initialized value probably broke your app logic. In Rust if I add a field to a struct, the compiler warns me about all the places that I need to double check.

> Would you mind sharing some of your experiences?

I highly recommend zero2prod book which is well-written, practical, but still teaches the essential principles (https://www.zero2prod.com/). You basically deploy a CRUD app to DigitalOcean from scratch. The best way to ramp up IMHO.

> How do you find the tooling?

Cargo is sweet. rust-analyzer is all I need. I need less extraneous tooling to be effective. For example, in Go I might use a task runner to watch the repo and run tests when I change a file. But in Rust I can just follow the rust-analyzer highlights and manually compile less-frequently.

> Also, I've considered Diesel for ORM, so was wondering if you've been using that too.

I was not happy with GORM (https://gorm.io/index.html) and never had a satisfactory experience with any ORM. I'm a fan of writing plain SQL, even in Go. It's just that with Rust sqlx I can get compile-time checks against the schema. It's not anything new (see Haskell), but it tightens the feedback loop and I have full control of the performance.

Thanks for getting back. I'll take a look at those resources you recommended. Certainly, working with Rust, a lot just 'feels right', it brings together elements I liked from other programming languages like Perl, Erlang, C. Those being: expressiveness, efficiency, a reasonably sane standard library, and functional goodies like immutability and closures.

> You add the field the the whole thing still compiles even though the zero-initialized value probably broke your app logic.

Ah, I found Python notorious for the same reason.

I've found ASP.Net's ORM to be quite good, though this is only with a year's experience, so perhaps I'm missing some cracks that might emerge later.

do you have experience with protobuf in rust? that's the main thing making me think twice about using rust in backend (protobuf doesn't have an one-true rust library)