4 ms·
I'd argue that even without the memory safety and outstanding tooling, Rust (at least no-allocator, embedded Rust) maps 1:1 with C and C++ with nicer syntax. S
by the__alchemist 5y ago
I'd argue that even without the memory safety and outstanding tooling, Rust (at least no-allocator, embedded Rust) maps 1:1 with C and C++ with nicer syntax.
Some examples:
- No DRY between headers and source files
- Arrays are explicitly described as such in type signatures, ie not as pointers to the type they contain
- No pre-processor
- Cleaner, more explicit pattern matching syntax
- No prefixing all constants/structs/enums with the module name
- A struct or enum is declared once, with its name; ie no _t, _e, _s suffix and name duplication
- Type inference
- Explicit handling of enum variants
- pjmlp 5y ago- use modules - use std::array and std::vector - Most stuff can be use with templates and constexpr - use namespaces - just like in C++ - auto type inference - enum classes It is about time to replace C with C++.
- the__alchemist 5y agoDidn't know that! I've been translating several C and C++ (mostly C) codebases (embedded), and haven't seen those. Probably because C, and older C++ specs / design patterns.
- AnimalMuppet 5y agoHow do modules solve the first one?
- pjmlp 5y agoThe same way as a crate file contains everything. https://github.com/pjmlp/RaytracingWeekend-CPP/blob/main/OneWeekend/RaytracingLib/material.ixx https://github.com/pjmlp/RaytracingWeekend-CPP/blob/main/One...
- pcwalton 5y agoPattern matching is much more powerful in Rust than in C++, even with Boost libraries. Type inference is likewise more powerful, as you can overload on return type.
- pjmlp 5y agoToday, by C++26 it should be there, while libraries like CUDA or frameworks like LLVM will keep using it. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2392r0.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p239... Type inference is powerful enough with generics being Turing complete.
- greggman3 5y agoWhy is a preprocessor bad? I do tons of platform level programming. Let’s say I’m doing Metal. There is #if ISO, #if APPLETV, #if MACOS stuff around the code. Sure, I could over engineer by trying to pass in some specialization functions but it can get extremely tedious and ugly to deal with. There’s a time and place for both. If I recall correctly, C# didn’t ship with one but it was added. For one it makes it easier to use the standard build system (just compile all the files referenced) rather than have to use an outside build system that knows when to include linux.xxx and when to incluide windows.xxx etc. Of course I’m aware of some of the drawbacks but I’d prefer with than without.
- Rusky 5y agoRust has equivalent functionality but it works on syntax trees rather than token streams, so you write `#[cfg(target_os = "macos")]` as an attribute rather than wrapping an arbitrary chunk of text in `#if`/`#endif`.