25 ms·
Sayonara, C++, and Hello to Rust
- CyberRabbi 5y ago> I plan on writing several posts about Rust features, why they’re an improvement upon C++ features, and why Rust is a better, more modern programming language. I’m always impressed that Rust turns people into such devoted evangelists after relatively little experience with it. This is a testament to its quality.
- usrnm 5y agoIf JavaScript frameworks taught us anything, it's that people tend to fall for "new and shiny" much more willingly, than for anything related to quality.
- MaxBarraclough 5y agoI think that's at least somewhat particular to the web, though. Plenty of 'C++ people' have been doing C++ for decades.
- ioseph 5y agoI'm not too sure that's still the case in the javascript world. React is coming on to 10 years and still remains the most beloved framework.
- rapsey 5y agoRust is not that new and shiny anymore.
- nlitened 5y agoIt has a new edition every year though :)
- rapsey 5y agoNo it does not. 2015, 2018, 2021. With the 2021 changes so small few projects actually required any changes to be compatible from 2018.
- nindalf 5y agoThe 2021 changes could be applied with an autofix tool - “cargo fix --edition”. Most of these changes are small paper cuts that make sense to fix, but they’re breaking changes so they need to be opted into explicitly. For example, earlier use a field x of struct b in a closure would capture the ownership of the whole of b. In rust 2021, the closure will only take ownership of b.x. This makes sense, but it’s a breaking change and therefore it needs an opt-in. The Edition system is one of Rust’s biggest strengths IMO. Some languages never make breaking changes be enough if they’re small because it would break too much code. Other languages make breaking changes but expect users to migrate, placing a burden on them. The Rust edition system leaves it up to the user, on a per-crate (package) basis. Staying on the old edition is perfectly acceptable, as is moving to the new one. Further reading - https://doc.rust-lang.org/edition-guide/rust-2021/ https://doc.rust-lang.org/edition-guide/rust-2021/
- GolDDranks 5y ago*every three years, actually. The current one is 2021, the one before that was 2018 and the (implicit) first one 2015.
- nindalf 5y agoMaybe that’s why they named it Rust. It sounds old and boring.
- FridgeSeal 5y agoFor a bit of trivia, I believe it’s named after a fungus.
- jayflux 5y agoThat’s not really what was happening in the JavaScript world though. It was a new ecosystem trying to figure itself out, and it takes quite a few attempts to do that. Both react and vue are near 10 years old, express is over 10 years, so it’s not “new and shiny” chasing. Rust will go through the same thing before the dust settles.
- flyinglizard 5y agoThat’s a very kind interpretation.
- rmnclmnt 5y agoWhen reading the Rust Book and coming from a C++ background, it feels like every chapter brings a solution to an issue you have encountered previously. So yeah I think this gives C++ programmers hope to not lose too much hair anymore! (C++ made me bald the first 5 years!)
- jstx1 5y agoWhen I see someone very devoted to a programming language, I see it as a sign that they might have problem with their perspective, not as a testament to the language's quality. Rust might be great (I don't know) but this aspect of the community makes me less likely to try the language, not more.
- gefhfff 5y agoWhy? ( I didn't care a bit about this aspect when I approached Rust )
- mgaunard 5y agoThe reality is that programming with a language like Rust or C++ takes a higher level of expertise, those are more sophisticated tools than web technology. You need to invest a lot more to use the tool well, but then the tool can do a lot more.
- tialaramex 5y agoIt's the first new language I've learned (in let's call it 30 years of programming, not counting Turtle Logo or self-taught BASIC) which made me want to rewrite my programs. I quite like several languages I learned, Java, Python, even C# is rather nice in some ways, others didn't make a dent, Scheme I had actually forgotten until I was asked about old Scheme code I wrote, ML was nice but too abstract for me... I am learning PowerShell at the moment for some reason, I can't say I'm fond. However I never had any inclination to go back and rewrite software. The software already works, why change language? But I've already spent a bunch of time doing that in Rust because it's so much nicer than the C I wrote a lot of software in previously. It's not just the language itself, which is pleasant, the tooling is excellent as well. I spent part of Sunday pair-debugging a friend's Perl AoC solution (it worked for the test input but not real inputs, that's a fun story) and they lamented that early on they'd forgotten to have revision control. When you ask Rust's cargo for a new project, it automatically creates you a git repository (if git is available) for the project, and if you didn't say you're making a library it gifts you a "Hello, world." example program to start with. The error messages are excellent. The developers actually put effort into further refining the already excellent error messages. After a long day writing SQL Server queries for which the response is typically "Syntax error" if you got anything wrong like this is still 1975, it's really nice to write a line of Rust that's wrong and have the compiler explain why it's wrong. The documentation is lovely. The official documentation for the standard library is in the exact same style as auto-generated docs for your own code. Because why wouldn't you do that? Java more or less did this and I appreciated it then, but writing such docs for Java is much more painful, and yet the results are less useful. C# is similar. I believe Swift got nice documentation behaviour in a recent version but I don't know Swift.
- happyweasel 5y ago> Who wants to type std::unique_ptr<Foo> when instead you can write Foo *? struct Foo{ std::unique_ptr<Foo> Ptr; } Foo::Ptr foo;
- HelloNurse 5y agoAngle brackets don't look good, but explicitly stating that the pointer is a unique_ptr rather than forcing the reader to figure out where Foo::Ptr is defined (presumably a different file) in order to look up the actual type has a lot of value.
- codeflo 5y agoCodebases that do that are very rarely const correct, however. (Personally, I don’t think typing unique_ptr<Foo> or unique_ptr<const Foo> is such a big deal. But then, I also consider having “using namespace std” the only somewhat sane way to use the STL.)
- omegalulw 5y ago+1. In modern C++ you use can use auto most of the time anyway :)
- tialaramex 5y agoThe thing about auto compared to Rust's inferred types is that when Rust isn't sure it won't infer anything and punts the problem back to you. let sandwiches = inventory.filter(delicious_cheeses).map(make_sandwich).collect(); ... won't compile because Rust can't infer what sort of container sandwiches is. Is it a vector? A list? Something custom? When C++ can't be sure in some places auto is obligatory anyway, so, too bad, you get what you're given. There are rules for what you get, but you might have reasonably intended something else, and so the effect is surprising.
- MauranKilom 5y ago> But then, I also consider having “using namespace std” the only somewhat sane way to use the STL. I hope you never ever do that in a header :)
- pjmlp 5y agoApparently the author never heard of C++ modules.
- glouwbug 5y agoSame goes for the gcc maintainers
- pjmlp 5y agohttp://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Modules.html http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Modules.html Granted it is not yet all there, but so it is with ISO driven languages.
- varajelle 5y agoEven though I heard of it. I'll believe it when i see it usable in practice. Just like "export template"
- pjmlp 5y agoHave fun, https://github.com/pjmlp/AStarDemo https://github.com/pjmlp/AStarDemo
- forrestthewoods 5y ago> Apparently the author never heard of C++ modules. I've been programming C++ professionally for 15 years. My work currently runs on Windows, macOS, and Android. With a little Linux on rare occasion. I've been hearing about C++ modules for, I dunno, 7 years? Thus far they have had literally zero impact on my both my job and my decision matrix. I do not anticipate they will be actually something I can use until at least 2024. I will be mildly surprised if C++ modules are in broad use in 2026. I would have said similar things about Rust until recently. I definitely can't replace all my C++ code with Rust. But I can use Rust to write new CLI tools and write certain libraries.
- pjmlp 5y ago
- lordnacho 5y agoGo and try it. I did it, productivity is much higher than cpp for me. For one, pulling down a package is a one liner. It just works, and I haven't come across a special one that does its own thing yet. The ecosystem seems pretty complete, too. I've yet to have to write my own component: everything from serialisation to async runtime, queues, config parsers, rate limiters, hmac, logging and so on is there. The big thing though is debugging. I'm CPP is possible to write bugs that manifest in very strange ways when you run the program. It eats up days to debug, and you're never sure if it's actually solved, because there's a large surface that might contain the bug. With rust it will complain at you at compile time. This can be confusing but you'll figure it out eventually and then it's fixed.
- jerrre 5y agoHow long ago did you change, and what kind if software are you working on?
- lordnacho 5y agoMost of a year intensively, was keeping an eye for a long time before. Real time multiplayer game side project, and a trading application. So stuff that needs at least some performance and garbage collection would be something with a real cost.
- fhd2 5y agoAll those crates are actually the main thing bothering me about Rust. I've seen some fairly trivial code bases that pull down north of 1 GB of barely necessary dependencies (a couple of wrappers and utilities). It's not quite as bad as NPM land where some people add a dependency for left padding a string, but dunno... Probably a fairly unpopular take, but I kinda always liked that about C/C++ code bases (that avoid hidden dependencies): It makes you realise the cost of dependencies :P So while Rust eliminates a lot of foot guns from C/C++, IMHO it introduces a major one.
- berkut 5y agoLikewise... I'm more and more finding myself either either looking for less-popular and more minimal crates (that don't need Async, or tens of other further deps), and even writing my own things for certain simple stuff (the latter often reinforced by wanted to experiment and learn). I'm more concerned about many 'critical' (because there's nothing like them in the standard library, i.e. 'rand', 'chrono') and well-recommended crates are still not at version 1.0.0, so technically they could change the API... (they won't/can't, but seems a very odd situation to me).
- rackjack 5y agoSeeing this made me realize my own honeymoon period with Rust is fading, if not already over.
- mdp2021 5y agoWhat of the article indicates Rust as deserving an only passing attraction? Or more in general: ...why?
- jerrre 5y agoIf I interpret you correctly you've switched to Rust and then after a while disliked it? Would be very interesting to hear your experience. Most of these articles like OP, come off exactly what you say: just switched, and now a big fan.
- tialaramex 5y agoNot everybody whose Honeymoon is over immediately gets divorced. You might go from believing that you've found somebody flawless to discovering that actually two weeks with them on a sandy beach or in a hotel room was maybe more exposure to their specific quirks than you are OK with, and you're quite glad to spend a few hours every day not with them - without that meaning you wish you were with somebody else instead. For example, you might start out very happy that Rust's assignment operators have no value, and so lots of trivial C and C++ mistakes can't happen, but as the Honeymoon wears off, you realise two things, firstly, that once in a while it would be convenient to use values of assignment, and so while on the whole it's better to be rid of them you do miss them, and secondly that some other mistakes are needlessly still present, why didn't Rust fix those too?
- bschwindHN 5y agoI've been using it since around 2015 and still loving it. I haven't earnestly tried to use it for a frontend project, but I've used it in many other types of projects and found it very enjoyable.
- zuj 5y agoThe more I see these kind of articles the more I am convinced that Rust is the future. Coming from a non technical background and being a python developer I wanted to learn rust but struggling to get comfortable with it. The main reason being I am not in a position to write anything practical with it apart form going through the book and reading tutorials. Gotta put more wood behind this one. I hate to speculate but most of the modern stack will be Rust, python and JS ?
- fileeditview 5y agoNo.. there is no "language x is the future". There are many languages for many purposes. Learn multiple, see what differentiates them. All of this hype needs to end.. seriously. Terms like "modern stack" should be forbidden. This all reflects in job descriptions. They just mention a ton of buzz tech, no matter if they actually use it, if you would actually use it in the job. No description of what you will actually do. This focus on the tech stack is superficial.
- zuj 5y agoI agree with you. Sometimes it's just so hard not to use the tech buzz words.
- CraigJPerry 5y agoThere’s an idea that you should work in the highest level language you can get away with for the task at hand. E.g. if you were writing a device driver, you might try to choose c over asm wherever you can. If you were writing an enterprise app, you might choose java over c++. Etc etc The idea is that you don’t want to give up productivity boosters like garbage collection unless you’re forced to. But it’s just an idea and not everyone buys into it. Otherwise we’d all be writing in the ultimate programming language (lisp).
- Mikeb85 5y ago> Otherwise we’d all be writing in the ultimate programming language (lisp). I like Lisp, but I'd argue that languages like Python and Ruby are higher level. Also easier to read.
- moth-fuzz 5y agoTrait objects are still no replacement for the areas in which subtype polymorphism excel, and Rusts' generics are a pittance compared to the completeness and expressiveness C++ templates. I know that sounds like flamebait, and yes I've read STL source code in all its underscored and abbreviated glory, and yes I've read that one snippet of the std::ranges proposal that people like to throw around. But the thing that always gets me is that most Rust proponents I've talked to simply define out of the equation Rust's problem areas - an oft-repeated mantra is that Rust 'pushes you to a good design' as if what is inherently a good design are what Rust is capable of and the areas it is less so are simply inherently poor designs anyway. Sure you can do everything you'd want to do in C++ in Rust, but the developer experience when actually writing code is agonizing most of the time. I deeply miss C++ templates in most other languages, excepting fully dynamic languages, and D. Rust is, to me, more of a replacement for places I'd write C, and be in a C mindset. C++ (well-written, modern C++) is so much more than C, and so much more than people give it credit for. A true C++ contender would have to pass not just the C bar but the C++ bar as well, rather than looking at C++'s strengths over C and saying "actually those are bad things" like it's been popular to do for the last 10 years. Speaking of replacing C, Rust is not without its strengths: the one place I find Rust truly a joy to write is OS dev, and similar low-level projects, in which the concepts and limitations of the language map to quite nicely to the expectations of the system. It seems like the obvious candidate for these types of applications (including embedded development) but there's little surrounding literature - it always baffles me that most people seem to be using Rust to write ... web services?
- fhd2 5y ago> it always baffles me that most people seem to be using Rust to write ... web services? I think it's the whole x-rewritten-in-rust-now-faster hype train. Although those limitations you mention, what's easy and hard to do, do have a more or less automatic impact on performance, fueling that train. I think of it like this: Programmer level 1: Makes something work in a higher level language Programmer level 2: Makes something work in a lower level language Programmer level 3: Makes something fast in a lower level language (in most cases just a few simple optimisations on top of #2) Programmer level 4: Makes something fast in a higher level language (object pooling, understanding how the GC and VM works to avoid GC pauses at the wrong time and all that) So with Rust (or yes, C++, though it's a bit more of a foot gun IMHO), level 2/3 people can write code that's efficient. What I think is somewhat unique about Rust is that it keeps programmers from making lots of common mistakes working with lower level languages - like it's designed for level 1/2 folks.
- rectang 5y ago> If you are a systems programmer, if you are used to C and C++ and to trying to solve systems programming types of problems, Rust is magical, just like when you learned your previous favorite programming language. Ok... > If you are not, Rust is overkill for your task at hand and you shouldn’t be using it. I earnestly recommend Haskell. That's... unusual advice. There's a lot to be learned from Haskell, but if I was recommending languages for the application space, there are other languages I might mention first.
- choeger 5y agoHaskell has the downside of being lazy and thus very unintuitive. OCaml, with multicore might be a better choice, if the community gets a tad more vibrant. But in general, I get the sentiment. For high-level projects, use a high-level language. And if you look at the sad state of js, python, et. al., there is plenty of room for improvement. Maybe it would even work if someone strapped Rust's type system on go or go's GC on Rust.
- jll29 5y agoI had to (ask my team to) port a system implemented in Haskell to Java because we couldn't find knowledgeable developers (people who knew Haskell + NLP). This was around 2006-7, not sure if it changed since. Any important codebase should rely on a language for which sufficient talent and community support (StackExchange...) exists, otherwise you add yet another risk to your project.
- choeger 5y agoWait a second. Your team was capable of porting from Haskell but incapable of maintaining an existing Haskell code base? Are you sure they just didn't want to use Java because of their own personal reasons?
- Zababa 5y ago> Haskell has the downside of being lazy and thus very unintuitive. Haskell uses monads for lots of stuff, which can be unintuitive too. From what I've seen, multicore OCaml will allow you to write programs in direct-style (instead of monadic-style) and still be async, which would be great. I hope multicore OCaml will be that "garbage collected Rust" that many people seem to want.
- mgaunard 5y agoThis article conveniently ignores that C++ modules exist, and that forward declarations are necessary in many scenarios. Then the rest of the article is just nonsense, complaining that you have multiple ways of doing something and the like.
- steerablesafe 5y agoC++ modules "exist." They exist in the standard, there are experimental implementation in compilers and there is little to no support yet from build systems (including cmake). I sincerely hope that it turns out OK, but it's not something usable in production yet.
- mgaunard 5y agoBy that standard the whole of Rust is experimental, and therefore not usable in production.
- pjmlp 5y agoThey work quite well on Visual C++ and MSBuild.
- steerablesafe 5y agoGood to know, I hope other toolchains catch up soon.
- Animats 5y ago(White on black in Courier? Please.) After a year of writing Rust for a client for a virtual world, some comments. For this problem, the server is constantly feeding you UDP packets full of changes to the scene, and you're constantly fetching assets from other servers using HTTP. All this has to happen while updating the screen at 60FPS or so. It takes several CPUs and a GPU working hard to show a detailed 3D world properly. * Safety is a big deal. I haven't had to go into a debugger in the last year. That's a huge win. * There's a lot of concurrency involved, and being free of race conditions is a big help. Rust does not help with deadlock avoidance, though. That's the next frontier in concurrency - static deadlock avoidance analysis. There are people working on this for Rust, but it's a hard problem. * Rust has a big problem with certain data structures. In particular, if you have a single ownership tree, you cannot get from a child to a parent. If you want to have backlinks, you have to reference count everything. The language needs something like "single owner with weak backlink, made safe at compile time". That's a hard problem. I keep ending up with refcounts forward and weak refcounts backwards. With refcounts, if you borrow something twice, you get a panic at run time. The compiler cannot catch those at compile time. So borrowing has to be very short term. * Rust wants functional programs. If what you're doing fits into that model, everything goes smoothly. When there's a lot of state, it's harder. The state here is a whole 3D scene, so it's inherent in the task. In server side web stuff, most of the state is in the database, so the application programmer doesn't have to deal with maintaining its consistency. * You can paint yourself into a corner with data structures. Figuring out where you need reference counts, where you need locks, and where you need interior mutability has to be right. And you can run into problems such as "that struct can't have the 'send' attribute because it has an element which, deep down, contains a reference count". Make a mistake and rework is difficult. Data structure design is a puzzle-solving problem. * "Async" has most of the same locking restrictions as threads. It's not like Javascript, where you never explicitly lock. Unless you have some huge number of threads, async isn't much of a win. It's really for the use case of a server with a huge number of slow clients. You probably shouldn't be using Rust for that anyway. That's Go's ideal use case. I have some arguments with the Rust designers. But really, it's a big advance over C++. It's not a panacea. As I keep saying, do your web server stuff in Go. Go has a more user friendly memory allocation model, a more user friendly threading model, and well-exercised libraries from Google for things web servers do. Rust is for the hard problems.
- _Understated_ 5y agoI can't help but think that with the way modern languages/programming are going, we're making a rod for our own backs! Don't get me wrong, I've read about Rust and it's capabilities vs things like C++ and yes, I can see benefits. That's not what I mean. It's in 10 years when we clone a repo on Github and it pulls down 2000 dependencies: will they all still work? Everything nowadays seems to require massive dependencies! I know we've long used libraries for different bits of functionality rather than reinventing the wheel but it just seems like we're giving up masses of control/agility for convenience that may kick the shit out of us later! Just a thought...
- fiedzia 5y ago> in 10 years [...] will they all still work? Most likely no. This is why Rust has its own repository of immutable packages on crates.io. Depending directly on Github is indeed a terrible idea.
- hoseja 5y agoI agree with pretty much every point besides the author's desire for significant whitespace. Please no. Whitespace is for people and readability, not the compiler.
- paradaux 5y agoSignificant whitespace is one of the things that irks me the most about the likes of Python, I'll take C-style braces over significant whitespace any day. I actually prefer the Rust approach here as well, removing the single-statement shortcut makes sense, as all it does in my view is cause issues, and I have it baked into my code styles for it not to be used.
- hoseja 5y agoYes, that is the sane choice.
- riskneutral 5y ago> The problem? Convenience. Who wants to type std::unique_ptr<Foo> when instead you can write Foo *? Why are the somewhat-deprecated options the easy ones to write? Why isn’t it something like std::raw_ptr<Foo> with some convenient notation for std::unique_ptr? It would be a terrible idea for Foo* to mean std::unique_ptr<Foo>. If you are typing out "unique_ptr" so many times in your C++ code, you're probably doing something very wrong. The C++ unique_ptr controls memory ownership, and you typically only want one owner of a resource. Sometimes it is necessary to transfer ownership of the resource, but it is (or certainly ought to be) a rare occasion in comparison to how often you need to access the resource. I could understand if you are passing shared_ptr around all over your code (although I wouldn't recommend that), but unique_ptr?? Also, raw pointers have not been deprecated in C++. Raw pointers are not a problem when you get them from a unique pointer. Raw pointers can be a source of errors when a single pointer is used to index into multiple objects, but for this C++ now has std::span<T> so you can avoid most of those common errors.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- elb2020 5y ago"The problem? Convenience. Who wants to type std::unique_ptr<Foo> when instead you can write Foo *?" I think this is an imagined problem. If you use Foo * for ownership, you will need to write new Foo() + delete foo. I don't really think that's more convenient than just writing std::make_unique<Foo>(). It's more a matter of old habit, I think. Still, if C++ was designed as a new language today, I think there's a chance that unique pointers would have made their way into the native language specification, together with for instance std::tuple, instead of being library features, like they are today.