Live data from Hacker News

Comparing Rust and C++

kukuruku.co

101–110 of 139 posts

Re: Comparing Rust and C++

#101

Rust looks really promising. Unfortunately, the tooling just isn't there yet, it seems. Release a working plug-in for YouCompleteMe and I will switch overnight ☺

It's true that Rust's tooling is lacking, but if you're just looking for autocomplete for vim, https://github.com/phildawes/racer#vim-integration exists. I haven't tried it myself, but I hear good things.

Re: Comparing Rust and C++

#103
post #23

Earlier quoted context omitted.

All the information needed to make these guarantees is the type information of all visible items in a translation unit ("crate").

I'm skeptical. Did they really make the type information stored in the ABI that advanced? Storing that kind of information outside of name mangling is a nontrivial feat (i.e. they'd need to rewrite the linker) and there's not a whole lot of information you can pack into name mangling.

They aren't stored in the mangled names. Types and other metadata on items are encoded into a special metadata file that lives in the archive.

Re: Comparing Rust and C++

#104

The one thing I don't like about Rust is the implicit return. Looking at code, it's easier to spot a missing/extra "return" than a missing/extra ";".

If you put an "extra" (unintended) semicolon on the last expression, the function will return Void/Unit: if this doesn't agree with the given type of the function, then there will be a compiler error. If you forget a semicolon, then the type of the function will become the type of the last expression, which will again make the compile complain.

Are there other issues that you had in mind?

Re: Comparing Rust and C++

#105
post #23

Earlier quoted context omitted.

All the information needed to make these guarantees is the type information of all visible items in a translation unit ("crate").

I'm skeptical. Did they really make the type information stored in the ABI that advanced? Storing that kind of information outside of name mangling is a nontrivial feat (i.e. they'd need to rewrite the linker) and there's not a whole lot of information you can pack into name mangling.

It basically generates a header file from the source code and then stores it alongside the object data containing the type information of all public items.

Re: Comparing Rust and C++

#106
post #10
post #6

It might be a good idea to compare C++ and Rust and not “C with Classes and no use of compiler options” and Rust. They do have a fair point about ugly template error messages, but the remaining issues are mostly moot: Freed memory issues can be avoided using std::unique_ptr and its siblings, lost pointers to local variables shouldn’t occur when using references instead of pointers, uninitialised variables are warned…

Good points. Also, first paragraph says C++11 (too be honest, C++11 mode ) yet first example has std::vector >::const_iterator . Where's auto , cbegin/cend ? In short: none of the C++ code written in the article resembles what a well-trained, well-behaved C++ programmer uses. And the same cannot be said of the Rust code, I think. On the other hand: maybe the whole point of the article is something like 'you don't hav…

Yes, auto will save you:

    std::vector features(const Widget& w);
    
    ...

    auto highPriority = features(w)[5];
    processWidget(w, highPriority);  //whoops, undefined behavior
This is from Scott Meyers' new Effective Modern C++, in the chapter about why you should use 'auto'. Even the new 'safe' and 'convenient' features are loaded footguns.

Re: Comparing Rust and C++

#107
post #12

Earlier quoted context omitted.

One thing important to keep in mind that unique and shared pointers are actually _much_ less common than borrowed pointers in Rust. I have little C++ experience, but I can borrowed pointers in Rust are used exactly like the unsafe pointers that would be used in the same situation in C. In other words borrowed pointers allow you to write the same code you would in C, but safer. [Other Rust features necessitate or stro…

What do you mean less common? Less commonly used or less commonly useful? Idiomatic C++11 discourages usage of raw pointers altogether. C++ introduced RAII ideas quite a long time ago, it's just in practice they weren't always followed and only in C++11 they were backed by standardized features from stdc++. Rust has an advantage of following this approach from the very beginning and not having all kind of legacy bagg…

To be clear: Rust has quite a few smart pointer types, but because Rust makes regular references completely safe, people just use those a lot of the time. For example, very few functions are written to take `Vec`s (the equivalent of C++'s `std::vector`), because they can simply take slices (`&[T]`) which are just a pointer and a length. Storing a slice in a structure isn't unsafe in Rust, so people do it all the time. The same goes for strings (Rust's `&str` is equivalent to C++'s `std::string_view`). It is also very common to pass references directly into slices, strings, or other data structures rather than copying, allocating, or moving, since it's completely safe to do so. You end up only really requiring explicit smart pointers for recursive data structures, or in very rare cases where you need complex ownership behavior.

Re: Comparing Rust and C++

#108
post #105

Earlier quoted context omitted.

I'm skeptical. Did they really make the type information stored in the ABI that advanced? Storing that kind of information outside of name mangling is a nontrivial feat (i.e. they'd need to rewrite the linker) and there's not a whole lot of information you can pack into name mangling.

It basically generates a header file from the source code and then stores it alongside the object data containing the type information of all public items.

So there's no run-time dynamic linking? Or is it possible, but only with additional metadata files present? Or maybe the assumptions are statically verified according to the interface and it is assumed that they are never broken by clients?

Re: Comparing Rust and C++

#109
post #96
post #61

Earlier quoted context omitted.

It's pretty well known that one should read Stroustrup's book and the Meyers books. If one were to learn in a vacuum, perhaps they would take a false path, but the community is in agreement on what the best learning resources are. As cute as your comment is trying to be, most languages cannot be learned by blindly trying things out, one should actually read a few books, try to be a part of the community and so on.

What are the reference of the books you are talking about ? Is it this two ones : http://www.amazon.fr/Effective-Modern-Specific-Ways-Improve-... http://www.amazon.fr/C-Programming-Language-4th-ebook/dp/B00... Note that they are both quite expensive. I am a student and it is not nothing to pay for this books.

I remember being a student and not having a big budget for books. Now I have a small apartment, and no space to put the books I have.

Abe Books ( http://www.abebooks.com/ ) provides a good place to look for used copies of books. A significant portion of the books in my library have stamps on the pages indicating that they were discarded from really nice institutions.

Of course there are always free-to-read blogs, but unfortunately, good C++ blogs are harder to find than other, newer language blogs.

Re: Comparing Rust and C++

#110
post #42

Earlier quoted context omitted.

These biased bashes against C++ that advertise rust get somewhere between boring and annoying recently. The main Rust community tries to discourage Rust "zealotry." For example, on /r/rust, the rules include: 4. When mentioning other languages, keep the discussion civil. No zealotry! Anyway, as for your other question: E.g. something like boost asio would be between hard to impossible to implement in Rust because the…

Regarding the futures: These are futures that you only can synchronously wait on (like what is currently available as std::future). However there is no possibilty to attach continuations that run when the value of the future is available. E.g. like the C++17 concurrency proposals, C#'s `Task` type or Javas `CompletableFuture`. These all rely on storing some kind of callback or closure inside the Future. That's not ea…

Interfaces can sometimes be tricky, especially if you're also working with lifetimes, because there's no way to write "T" for a generic type. There are are few other gotchas like this. Some of these will get better once associated types land. But yeah, trying to simultaneously manage interfaces, lifetimes and callbacks sometimes requires serious Rust knowledge—and at least for now, you may run into hard barriers.

You need some mutable objects (like socket or the object that contains socket) locally AND you need access to them in your continuation.

If you're planning on writing to objects from inside a continuation, while you also write to them from your main code as well, you're going to have to think things through pretty carefully in any language, or else the two writers will stomp on each other. Rust has lots of tools for locking and controlling access.

This will only work "safely" with garbage-collected or ref-counted types and these do still not seem to work for interfaces.

I'm not quite sure if I understand the problem you're describing. I can definitely share mutable objects between threads, and I can do so using abstract traits (aka "interfaces"). Here's a runnable example using Rust's "play" server: http://is.gd/bQOY3o

I had to use a "box" here, because that's the easiest way to store objects of varying size that implement a specific interface. Experienced Rust developers may be able to simplify this a bit.

Anyway, is this what you were thinking about? Or did you mean something else?

Post reply on HN