Hacker News new | ask | show | jobs
by osrec 2751 days ago
I've never tried Rust or Rocket, but it appears to be gaining popularity, so I have the following questions for existing users:

What's it like to work with? Do you prefer it to JS/PHP/Python for web related projects?

Can you iterate on code quickly, or is there a compile step on every iteration?

Are there stable libraries for interacting with MySQL/Redis/Postgres asynchronously?

Is there good IDE support, for example in Atom?

Thanks!

6 comments

It's a bit rough to work with still - the IDE support is still a work in progress (though improving) and there is a compile step with every iteration (incremental compilation has improved this too).

Diesel is the main library for interacting with SQL databases, it wasn't async last time I used it, and a quick web search tells me it still isn't - this has been a BIG problem with Rust over the last year, as everyone waits for the async implementation to finally become stable.

Rust is a funny beast at the moment - you have to deal with the learning curve, you have to deal with the unstable/pre-1.0 libraries (some of which are fine), you have to deal with all the errors the compiler throws at you.

But once you've done all that, something magical happens: you end up with software that is mind-blowingly stable and blazingly fast. It doesn't throw segfaults, it doesn't leak memory, it just works, and works amazingly well.

Isn't diesel being sync largely hid behind a connection/thread pool? Generally you want to limit the connections to the database anyway, so a connection pool is desirable...
Aren't those orthogonal concerns? A connection pool lets you share the same connection across multiple threads of execution. Async lets you share the same thread of execution across multiple actions waiting for IO to happen. For the best performance, you want both.
>it doesn't leak memory

Fairly sure you can leak memory, just no memory safety issues (in safe Rust).

You can also cause segfaults, the point I think is more that it is hard to do this instead of easy as it is in some languages, like C.
Memory leaks can happen in safe code, segfaults cannot. That’s at least one major difference.
That's a great summary, thank you. What about the rate of improvement? Do you think it will be significantly more developer friendly in a few months?
Futures should be in Rust within a few months and the next Rocket release will switch to async. The Rust plugin for JetBrains IDEs is also getting better every release[1].

In short, I'd say Q2 2019 would be a time to seriously jump into it if you don't want to do it now.

1 - https://intellij-rust.github.io

I've bumped around a site idea I've been working on for five years now. I started with Flask, then Django, then felt bad about page load times so I looked for something faster.

That basically made me shelf it for about two years, until last year when Rocket was rising and I gave it a shot. Got hung up again that year due to missing expressiveness in query handling and shelved it again. In the last month I picked it back up and have been working on it again and I must say - this is the best server side development experience I've had, either on the side or working with node / rails / etc for money.

I also definitely feel the performance bump, even without async in yet, and scaling Rocket apps is much more trivial than trying to scale Flask / Django via multiprocess.

I've also been spoiled by the Rust compiler and RLS, despite its breakages its still some really impressive stuff with how if you can get something to compile it is substantially more likely to work and not have errant edge cases that something written in a less prudent language would struggle with.

I haven't really gotten involved in any larger scale Rust projects or took on a maintainer role on anything substantial - I've just made a few patches to Rocket and some support libraries - but I can easily imagine this is the best kind of environment to do collaborative work in. You get rustfmt for style, you get the best compiler ever for debugging, and the project management of Cargo is just impossibly elegant and delightful over anything else I've used. Its definitely something I'd love to work in at a day job as it matures onto the market.

> with how if you can get something to compile it is substantially more likely to work

That is what expected the least, when I was starting out. I was looking for something that is a little like Python, but faster than it. It gives me a sense of security to see the compiler call me out for a reason, telling me exactly which cases I forgot. Together with the type system I often find myself in situation where I do something quite complicated (for my experience) and then I am at a point where I am confident it works, I `cargo run` the whole thing and it works like a charm.

In Python a lot of things would run, but also broken things. Having it run was no guarantee at all it won't end up in a convoluted traceback later. In Rust having it run is a great part of the deal, I rarely encountered Panics afterwards, and when I encountered them it was me willfully defining where I would encounter them.

The weird thing is: because it takes much more to just get a program that compiles – even if you're lazy you end up with a higher code quality. It won't let you take some shortcuts that will haunt you later and that is a good thing.

I also find that Rust drives me to document more - because when I do something "intuitively" that doesn't work and then spend an hour refactoring until it does work I usually leave a comment about it.

And those are almost always the good "in depth" reasoning comments you want in software, not the mandatory "Function save saves the file to where parameter location is" which just repeats the definition.

I still love Python, but I can't even argue its a productivity win for me to use over Rust anymore, because while I might sometimes make something that seems to work faster the lack of confidence makes scaling the program much harder and there really is nothing syntactically in Rust besides the static typing (which is a good thing in my book anyway, its why Python added function signature type hints after all) and verbosity (semicolons, mandatory braces, etc) that prevents you from writing code as fast as a Python variant with the right library support.

"with the right library support"

Well that's the thing it's all about libraries, and now in Rust it's nowhere compare to Python for the web dev.

> What's it like to work with? Do you prefer it to JS/PHP/Python for web related projects?

Of the popular frameworks in languages you mention, Rocket is fairly close to Flask in terms of feel. Rust is a typed language and Rocket uses code generation and function's type signatures to automatically check the incoming parameters.

This can be fairly simple, like verifying+converting a param to a number. It can be fairly involved like setting it up so that if one of your handler function's arguments is an AdminUser, Rocket will take the user id from the request/cookie, connect to the database, and verify that the user is an admin. This is doable with middleware in many frameworks for the languages you mention but the difference is that this is on-demand based on the argument types.

> Can you iterate on code quickly, or is there a compile step on every iteration?

Rust is a compiled language. There's a compile step when you make a code change.

> Are there stable libraries for interacting with MySQL/Redis/Postgres asynchronously?

There are libraries for interacting with all those things. That said, the Rust async ecosystem is in a transition period at the moment. If you're familiar with the JS transition from callbacks to Promises to async/await, it's basically that. Check back in 6 months.

> Is there good IDE support, for example in Atom?

IDE support in Rust isn't particularly good yet. In general, find references and go to defintion work but autocomplete does not.

> Rust is a compiled language. There's a compile step when you make a code change.

That said, it really depends on what you're doing. If you are debugging something, it can be a bit painful but not terrible (typically, you're just changing a few things as you go along). If you're implementing a feature or refactoring, you're better served using "cargo check" (especially in combination with "cargo watch") to only run typechecking but not code generation, which means extremely fast feedback.

> What's it like to work with? Do you prefer it to JS/PHP/Python for web related projects?

Coming from dynamic languages it will feel very different, especially if you don't have a lot of experience with typed compiled languages. Personally, I came from a JS background and I love it. It totally sold me on the benefits of a good type system so much so that I sorely miss it when working with JS now.

Writing web servers in Rust is a fairly new space, it's not as mature as js/php/python, however things are moving quickly. Having your code run orders of magnitude faster is pretty nice too.

> Can you iterate on code quickly, or is there a compile step on every iteration?

I think conflating 'working on code quickly' with compilation is a mistake, however, yes, you do need to compile your code. IMO a compiler will help you iterate more quickly because it's able to check for errors before you run your code.

> Are there stable libraries for interacting with MySQL/Redis/Postgres asynchronously?

If you want an ORM the thing you want to look at is diesel. I'm not sure if it's async or not.

> Is there good IDE support, for example in Atom?

rust language server (RLS) has plugins for most editors. I think the best experience for developing rust right now is in VScode or vim, IMO.

> Are there stable libraries for interacting with MySQL/Redis/Postgres asynchronously?

> > If you want an ORM the thing you want to look at is diesel. I'm not sure if it's async or not.

Diesel isn't async yet, but interfacing with it in async frameworks such as actix seems to be pretty straightforward: https://actix.rs/docs/databases/

>I think the best experience for developing rust right now is in VScode or vim, IMO.

Intellij + Rust Intellij plugin is probably the best, in terms of actual usefulness (code completion etc) and stability.

I really dislike intellij, so I've not spend much time trying their rust plugin. If memory serves according to the rust survey, vim or vscode are the most widely used environments.
I am a long time Pythonist and I started with Rust a year ago. Rust has become my goto language now. I mostly use python with the interpreter nowadays (as a enhanced calculator) or for scripting.

Rust has such a great tooling. In Python starting new projects, remembering whether to use pipenv pyenv, pew, pip virtualenv, which file was supposed to mean what feels much much more convoluted. In rust this is a two word shell command.

The compiler will fuck with you more than you would like, but if you don't fight it, but try to figure out why it is right, you will find that most compile errors are totally for a reason. I had FAR less runtime errors with rust. If the code compiles it is either code with a logic error or it just works as intended.

And it is performant as hell.

I did a lot of stuff with Django and Flask. On current project I was free to choose a platform, so I went with Rocket.

The framework itself is similar to Flask in productivity and amount of boilerplate, though it's even better. It converts all input data to proper types for you using type signatures of handlers. So I don't need to convert strings into ints, or into json and then check if it conforms to a schema. If handler is called, input is parsed and converted.

Rust itself is more verbose than python. Typical data manipulation, like put something in dict, extract/count uniques, "transpose" a datastructure, load data from file, usually takes 1.5x-3x as much chars. For this price you get strict compile-time type checks that take lots and lots of mental load off code refactoring, C++ level performance, and real multithreading, including easy data parallelism with libs like rayon. Rust also has enums (aka algebraic types or tagged unions) that can help to represent problem space better and make fewer logic mistakes later.

It would be all unicorns and rainbows for me if a more concise database interface was awailable. Currently the go-to ORM is Diesel, and it's very verbose comparing to, say, python's sqlalchemy. Current solution is to move database access from views into methods of a model.

About iteration: code requires recompilation. Changes to non-compiled templates do not. I use Maud templates and they make html look suprisingly clean, they compile and are fast, but well, you need to recompile them every time. Currently debug build takes ~10s, though parsing and typecheking are done in the first several seconds.

Diesel doesn't support async yet, AFAIK. But async is usually used to save on context switches and stack space, allowing to support tens of thousands of live connections. You won't ever have tens of thousands of connections to database like Postgres. 100 connections is quite generous. 500 connections may be possible on the fattest DB boxes. Postgres is not async itself, it allocates a 4MB scratch buffer for each client from the start, IIRC. The only good reason to use async for communication with postgres may be if all of your other code is async and you don't want to deal with sync/async impedance mismatch. Though you'll likely want have connection pool anyway.

About IDEs I can't say much, I use emacs with LSP, it works most of the time.