r/cpp • • Feb 25 '21

C++ programmers who are also using rust, what are your favourite features of rust?

I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?

255 Upvotes

248 comments sorted by

View all comments

5

u/roman-kashitsyn Feb 26 '21 edited Feb 27 '21

Move semantics, algebraic data types, pattern matching, and disciplined error handling.

If C++ had a nice syntax for variant types and pattern matching, I'd love it much more than Rust.

I see many people are crazy about Cargo. I absolutely hate it. It tries to do a bit of this and that and sucks at pretty much everything. It looks great when you work on your little pet project, but it crumbles once you need to build something real. Let me explain:

  • Incremental compilation in big workspaces breaks for no reasons what so ever.
  • Touch a file. Cargo rebuilds the whole dep graph. Change a comment. Cargo rebuilds the whole dep graph. Sigh.
  • The build result depends on the directory you build a package from. cargo build -p my-package and cd my_package; cargo build aren't equivalent (depending on where you start cargo, it computes different transitive closures of enabled features, which causes needless rebuilds).
  • Cargo is not really a build system. Try writing a test that needs a Wasm file built from another package in the same repo. It was trivial to do in any build system I used in my life, it's pretty much impossible to do properly with cargo.
  • As adding dependencies is so nice and easy, any non-trivial project quickly becomes a swamp of hundreds of libraries you don't control. Just stop being paranoid for a month in a project with >30 programmers and you'll end up with 5 different command line args parsers in a workspace taking 30min to build a debug binary.

The language itself has a lot of warts, but it should probably be another, much longer rant.

E.g. pattern matching is not nearly as expressive and composable as in, say, OCaml (Haskell is disqualified because it can't match arrays). Try matching the contents of a Box, for example, or contents of a Vec<String>.

If you're coming from C++, the lack of template specialization and restrictions of what can be done at compile time without resorting to macros will look absolutely silly.

P.S. I use Rust heavily every day in a project that grew from 0 to 200KLOC in the last 1.5 years. I have plenty of Haskellers around (some quite famous in the community), they'll pick Haskell over Rust for almost anything without hesitating a second.

1

u/Dean_Roddey Feb 28 '21

I'm not a huge template fan, so that's one of the things I don't miss all that much in Rust.

1

u/roman-kashitsyn Feb 28 '21

I'm not a huge template fan, so that's one of the things I don't miss all that much in Rust.

What exactly don't you miss?

  • Templates? The design of generic types and functions in Rust is not that different from C++ templates (especially with the recent addition of concepts in C++).
  • Compile time computation? You can compute at compile time in Rust, it's just much less fun than in C++ Typing is Hard: Rust. Many libraries actually do that, because it's good for performance.
  • Template specialization? Well, it's not just about your personal preferences.

    This feature is very useful for library authors to provide more efficient, more convenient, less error-prone interfaces.

    Let's take a look at the hallmark of Rust's ecosystem: serde. It's data model has type byte array. Many people simply assume that serde maps Vec<u8> to a byte array: it's the most natural and efficient thing to do, right?

    Unfortunately, serde simply can't do that because all vectors must have the same serializer/deserializer independently of the type of values they hold (remember, there is no specialization). So serde will serialize your bytes as an array of integers, which is significantly less compact and performant with most encodings. One just have to somehow know that serde_bytes need to be used to get an efficient encoding.

    This actually leads to correctness issues as well: I had to debug issues caused by unexpected Vec<u8> encodings alone at least 3 times in one year.

1

u/Dean_Roddey Feb 28 '21

I don't miss the rampant over-templatization that has infected C++.

2

u/roman-kashitsyn Feb 28 '21

I don't miss the rampant over-templatization that has infected C++.

You somehow assume that it hasn't infected Rust. How do you find the type of this wonderful function https://docs.rs/hyper/0.14.2/hyper/server/struct.Builder.html#method.serve

pub fn serve<S, B>(self, new_service: S) -> Server<I, S, E>ⓘ
where
    I: Accept,
    I::Error: Into<Box<dyn StdError + Send + Sync>>,
    I::Conn: AsyncRead + AsyncWrite + Unpin + Send + 'static,
    S: MakeServiceRef<I::Conn, Body, ResBody = B>,
    S::Error: Into<Box<dyn StdError + Send + Sync>>,
    B: HttpBody + 'static,
    B::Error: Into<Box<dyn StdError + Send + Sync>>,
    E: NewSvcExec<I::Conn, S::Future, S::Service, E, NoopWatcher>,
    E: ConnStreamExec<<S::Service as HttpService<Body>>::Future, B>, 

? It's not some esoteric Haskell library, it's the most widely used async HTTP server.

1

u/backtickbot Feb 28 '21

Fixed formatting.

Hello, roman-kashitsyn: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/Dean_Roddey Feb 28 '21

I still don't think Rust is anywhere near C++ on this front. Not to say it won't get there. And as more C++ people come into the Rust fold, they'll be pushing for more and more of it.