Live data from Hacker News

Switching from C++ to Rust

laplab.me

61–70 of 289 posts

Re: Switching from C++ to Rust

#61
post #19

Earlier quoted context omitted.

so, um, unions? i have to say that in many years of programming, i have almost never needed to use such types.

I can’t imagine not having the ability for a variable to be one of multiple potential types. I could probably work around it, sure, but why would you want to?

i can't imagine why you would want that. for example, why would i want something defined as an integer hold something other than an integer? this is how strongly-typed languages work.

Re: Switching from C++ to Rust

#62
post #43

Earlier quoted context omitted.

Yeah. In the C++ codebase I work on, we use folly::Expected (essentially similar to Rust's Result ) heavily. Is it as ergonomic as Result is in Rust? Absolutely not. But it's still a nice design pattern for some of the same reasons people use Result in Rust. (And C++ std::optional is somewhat similar to Rust's Option type.) https://github.com/facebook/folly/blob/main/folly/Expected.h...

Sounds like Google/abseil's "StatusOr', but maybe a bit more freefrom? https://abseil.io/docs/cpp/guides/status

Yeah, we use a fixed-E alias of folly::Expected in our codebase which is similar to absl::StatusOr. Something like:

  namespace our_project {
    class Status { ... };
  
    template 
    using Expected = folly::Expected;
  }
absl::StatusOr looks a lot like our_project::Expected. This pattern of providing your own error type and aliasing Result is also somewhat common in Rust, I believe.

Re: Switching from C++ to Rust

#64
post #2

Nice to see some level-headed anecdotes on the experiences of actually _using_ each language, as opposed to the usual "z0mg rustc fixez all yur mem0ry bUgz!!111one" And, as someone who's spelunking in the depths of a large CMake project right now, cargo sounds pretty nice.

Cargo is definitely a more pleasant build system than anything the C/C++ world has, until you fall off the happy path.

One of the pain points of trying to design a build system for C/C++ is that a lot of projects do weird stuff that needs to be supported in build systems, which means you end up needing escape hatches to do that weird stuff all over the place. Cargo takes a narrower view, which means it doesn't attempt to do things like package things for install or handle multi-stage builds. It also has the advantage that things like running tests or package dependencies were built in from the start, so it doesn't have to try to support a million different tools that provide that functionality.

Re: Switching from C++ to Rust

#65

Earlier quoted context omitted.

A union in C++ is the same thing as a struct, except all of its fields live at the same offset. So you can define any method you want on it, including special stuff like constructors and destructors. No base classes are allowed, though.

A method on a union is much less useful when you can't match on the tag though. I suppose you could store tag inside the union. Is that common? I've always imagined C/C++ tagged unions would store tag outside the union.

Yes, it’s very common. The difference is rust enforces via the type system that you match on it.

Re: Switching from C++ to Rust

#66
post #19

The call out to sum types is something I feel. I've been using Rust daily for almost 10 years now, and sum types are absolutely still one of the things I love most about it. It's easily one of the things I miss the most in other languages that don't have them. I'm usually a proponent of "using languages as they're intended," but I missed exhaustiveness checking so much that I ported a version of it to Go[1] as a sort…

so, um, unions? i have to say that in many years of programming, i have almost never needed to use such types.

In my experience with languages that lack concise sum types and pattern matching, you end up with data types that have lots of implicit invariants. If you find yourself writing docs or thinking in your head things like "Field X is only set if field Y is true" or "If field X is non-null then field Y must be null and vice-versa", then these are indications that sum types would model the data better. Then these invariants can be followed by construction, and it becomes clear what's available at data access - great for interface clarity, robustness, dev tools, morale, etc.

Relatedly, storing booleans is a smell, imho typically an enum or sum type is always better in languages that have concise syntax for these. True and False are meaningless without context, and so can easily lead to errors where they are provided to a different context or generally misused due to a misunderstanding about the meaning.

Re: Switching from C++ to Rust

#67
post #58

Earlier quoted context omitted.

> I've been using Rust daily for almost 10 years now, I'm just nitpicking but Rust is 7 years old and 7 doesn't feel almost 10... Edit: Rust hit 1.0 7 years ago. Now I feel silly.

If you’re going to nitpick you better be right, and you’re not. - and I think he might be too humble to say anything but you might want to look into who you’re replying to and their body of work. Rust has been around for well over a decade - the first “stable” release was 7 years ago. It’s always been opensource - any random hacker could download it and start using it since it was available. I think you will find kin…

He did replied, and I apologize for being ignorant.

Re: Switching from C++ to Rust

#68
post #42

Earlier quoted context omitted.

The union part is just an (obvious) optimization. An implementation detail. Sum types are not required to use a union; they can be implemented without using a union.

how, exactly?

struct List {}; struct Cons : public List { Cons(int v, List* nxt); int v; List* nxt; }; struct Nil : public List {};

This is how you'd do it in Java.

Re: Switching from C++ to Rust

#70
post #61

Earlier quoted context omitted.

I can’t imagine not having the ability for a variable to be one of multiple potential types. I could probably work around it, sure, but why would you want to?

i can't imagine why you would want that. for example, why would i want something defined as an integer hold something other than an integer? this is how strongly-typed languages work.

Scott Wlaschin's "F# For Fun and Profit" website is a great resource for strongly-typed (yes) data modeling with sum types as a fundamental tool.

https://fsharpforfunandprofit.com/series/designing-with-type...

By the third entry in the linked series, we've motivated the use of a sum type with three variants in the example domain model.

Post reply on HN