6 ms·
This can also be provided by C++ by .at() or iteration through containers.
by stochastic_monk 8y ago
This can also be provided by C++ by .at() or iteration through containers.
- lewisinc 8y agoI'm not super familiar with C++, however Rust has an advantage over this example as it avoids iterator invalidation at compile time by assigning the iterator a lifetime.
- pjmlp 8y agoWhile not the same thing, some C++ compilers like Visual C++ do that at runtime in debug builds.
- bluejekyll 8y agoIf we’re talking about switching to a new language from C, why only go almost safe with “modern” C++ and not all the way to fully safe Rust? I’m not trying to be snarky, but the thread was about Rust as an improvement to C and you just threw C++ into the mix... which isn’t as safe.
- unsatchmo 8y agoI like Rust, but based on the difficulty I’ve had writing even the most basic of bare metal software in it, it will be like 10 or 15 years before the community and the tooling are ready to do kernel or device driver work in Rust. C may have a lot of foot guns but it’s really easy to learn and use.
- bluejekyll 8y agoRust took me longer to learn than most languages, I'll grant you, but I'd say after 3-4 weeks I was writing safer code than anything I ever wrote in C. Not claiming to be an expert in C, but I paid my share of blood and tears with it, and all I can say is that it's worth the effort to learn Rust for the huge guarantees the language gives you in the same contexts as C. If you're interested in baremetal, there are a number of tutorials out there, all focused on different things. I really love the TockOS tutorials, they are really awesome for getting across some of the different benefits at different levels: https://github.com/tock/tock/tree/master/doc/courses/rustconf https://github.com/tock/tock/tree/master/doc/courses/rustcon... And then more raw OS stuff: https://os.phil-opp.com/ https://os.phil-opp.com/
- pjmlp 8y agoRust is already at a good level when you are writing low level OS infrastructure code and CLI utilities. For GUI software or GPGPU development, it still needs to mature a bit more.
- bluejekyll 8y agoYes, though I think patterns are starting to emerge that will start taking hold. For example: https://raphlinus.github.io/personal/2018/05/08/ecs-ui.html https://raphlinus.github.io/personal/2018/05/08/ecs-ui.html I mostly think this is a gap in common libraries at the moment and decent coding practices. We’ll see what happens with all of that. Personally I’m more focused on web-backends, micro-services, and DNS, so GUIs don’t generally present problems for me.
- pjmlp 8y agoThanks for the link, it is quite interesting read.
- unsatchmo 8y agoI’m gonna have to disagree with you there: once you turn off std, you’re off the map. Building a syscall handler for example, or implementing VM or even a simple bootloader is non-trivial and has taken me like 10x the time it would in C. If we want rust for device drivers, we need a more sane method of implementing std below userspace. The fact is that C is dead simple and currently contorts more easily. The cognitive load is two orders of magnitude less. Until Rust is this easy, we will use C at work.
- pjmlp 8y agoGiven that I loved doing systems programming in Turbo Pascal, Oberon, avoid to use C as much as possible, focus on Java, C# and C++, I do love a bit of straitjacket in name of security. I do agree though that it might need still some ergonomic improvements to earn the hearts of other developers that don't share this opinion.
- duneroadrunner 8y agoDon't forget about SaferCPlusPlus[1]. Much safer than conventional modern C++. Also (optionally) uses scope lifetimes to prevent use-after-free. (Not yet fully compiler enforced like Rust, but hopefully that'll be coming at some point.) There is a translation helper tool[2] that, at this point, can automatically replace native C array and pointer iterator declarations with macros that make them memory safe (bounds and use-after-free) when compiled in C++ mode. [1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus/blob/master/README.md https://github.com/duneroadrunner/SaferCPlusPlus/blob/master... [2] https://github.com/duneroadrunner/SaferCPlusPlus-AutoTranslation https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
- blub 8y agoActually the thread was about C and someone threw Rust into the mix, as is typical. One could go to almost safe C++ because C code compiles with a minimum amount of changes and turning on compiler warnings, using vector, string and smart pointers already gives a massive saftety improvement, without a complete rewrite in a language that's quite different from C, like Rust is. Smaller costs and still significant benefits in other words.
- bluejekyll 8y ago> Actually the thread was about C and someone threw Rust into the mix, as is typical. I was referring to the thread that started a conversation about pointers and safety in Rust, not to the general post. You are correct that C and Rust require some more bridging between the languages, but C++ also requires this when going from objects to C datatypes and vice versa. Mostly you save a little bit on the header definitions, but even there, the headers are generally #ifdef'ed to almost illegibility. With Rust's bindgen tool, getting C headers into Rust is actually quite easy: https://rust-lang-nursery.github.io/rust-bindgen/ https://rust-lang-nursery.github.io/rust-bindgen/
- blub 8y agoOk, so then we could agree that the more general general topic is safety, so different options could be discussed: D, Ada, Rust, C++, etc. The bindgen tool allows integrating C code with Rust code, which means than an existing C code base can be replaced with Rust step by step. A C code base can be compiled with some modifications as C++ and then it can be slowly refactored to use safer C++ idioms. In this case the safety improvements will be available much faster, even if the resulting code base won't be as safe as a Rust code base. The culture shock will also not be as large for the C programmers. These are non-negligible benefits, and a big reason why C code bases are moving to C++.
- stochastic_monk 8y agoThere are fewer safety guarantees even in modern C++, but there are times where Rust’s safety requirements do require slower code. Depending on why you were writing C, C++ can be the better choice, even though it can be harder. Where I stand, my biggest reason for not yet having used Rust for any projects is that the overhead for learning it outweighs what seems like a negligible gain for me when C++ already meets my needs.
- pjmlp 8y agoAnd if using Visual C++, operator[] and iterators are bounds checked in debug builds.