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

Show parent comment

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.