4 ms·
I don't know. I recently took a Rust course and our day three project was to build a simple multi-threaded webserver. The really easy kind in C where you acce
by jandrese 7y ago
I don't know. I recently took a Rust course and our day three project was to build a simple multi-threaded webserver. The really easy kind in C where you accept a connection and then spawn a thread to handle the request so the main thread can get back to accepting new connections ASAP.
This turned out to be quite challenging in Rust, even when starting from a working single threaded server. Threading the needle on borrows was quite a challenge.
- steveklabnik 7y agoInterestingly, a multi-threaded web server is the final chapter of "The Rust Programming Language" (full code is at the end of https://doc.rust-lang.org/stable/book/ch20-03-graceful-shutdown-and-cleanup.html https://doc.rust-lang.org/stable/book/ch20-03-graceful-shutd... ) It ends up being pretty short, though we also build a threadpool by hand. If you used a package, it'd be even smaller. (I like scoped_threadpool myself) (And the FnBox stuff will go away once I write an update for it, it's no longer needed.) It does the same: grab a connection, spawn the thread. There's virtually no borrowing.
- fluffything 7y agoDid you learn C (or any other programming language) by writing a multi-threaded web server in it ? The Rust book actually ends by showing you how to do that, but... there are many reasons why that's the last chapter in the book. Starting there is definitely not the most efficient way of learning Rust.
- tick_tock_tick 7y agoI did for golang. Assuming you know how to program in general it's a very basic task.
- vertex-four 7y agoEhh... it's a basic task as long as you never touch shared mutable state. As soon as you have to do that, you need to work out what the abstraction for dealing with shared mutable state in your language of choice is.
- tick_tock_tick 7y agoI mean most languages you don't. You lock do some stuff then unlock. Rust prevents a whole lot of bugs by not making it this simple but that means if you pick anything with threading as a task to jump into the language your going to have a much harder time.
- fluffything 7y agoRust `Mutex`es are simpler to use than C++ ones.. so if you just `Mutex` your shared state away, writing an app on top of `actix-web` or `hyper` or `tokio` is dead easy once you learn how to use the framework, particularly if you have already done that before in Go.
- fluffything 7y agoDid you just use Go, or did you write your own Go implementation including its goroutines run-time and then write a web-server on top? Because if you did just use Go, in Rust you can just `cargo add actix-web` (or `tokio`, `hyper`, `async_std`, or any other goroutine-like run-time) and write a multi-threaded web-server in approx. one line of code. What the Rust book example shows is how to implement what the Go language uses inside its run-time for running goroutines, which is a much harder thing to do, and very different, even though that's something that every good web-server uses somewhere in its stack. Learning Rust by using `actix-web` to build a simple web-server is ok, but learning Rust (or any other programming language, really) by implementing `actix-web`, is not a very efficient way of learning the language.
- kodablah 7y agoI had the same initial reaction. I wanted a tokio UDP server that delegated to other things. Working w/ tokio streams, futures, async (which I understand are all still in beta) was quite challenging to make sure they could all get back to the read/write of the UDP socket. Basically I end up having to split UDP read/write then concat/join disparate streams or use mcsp channels to fan everything out and get it back to a single stream.
- _bxg1 7y agoHaving used Rust for several months, I'd say that multithreading is still one of the biggest pain points (strangely, as it's also one of their headline features). The only thing I've encountered so far that's harder is pointer-juggling data structures, but at least in that case you can mostly just use the (excellent) ones in the standard library. The "crossbeam" library helps a lot: https://docs.rs/crossbeam/0.7.2/crossbeam/ https://docs.rs/crossbeam/0.7.2/crossbeam/ It's the only way I know of to have thread closures with a less-than-static lifetime, which helps a ton with borrowing/moving and also general encapsulation: https://docs.rs/crossbeam/0.7.2/crossbeam/fn.scope.html https://docs.rs/crossbeam/0.7.2/crossbeam/fn.scope.html. Once you get the hang of it you can move forward pretty well, but the ergonomics around Rust concurrency, especially in the standard library, really need some work if they're going to continue focusing on it in their marketing materials.
- pjmlp 7y agoPersonally I think languages with tracing GC are much more productive for those kind of scenarios.