4 ms·
The Rust version of ixy seems to have as many or a bit more lines of code than the C version? That is a bit suprising to me. 1306 for Rust, and '1000' for C. Th
by jononor 8y ago
The Rust version of ixy seems to have as many or a bit more lines of code than the C version? That is a bit suprising to me. 1306 for Rust, and '1000' for C. The Rust additionally uses 4 dependencies from crates.io
Unsafe usage is quite small, at 10%, which seems good for a hardware device driver.
- carlmr 8y agoDoes Rust claim to use fewer lines?
- swiftcoder 8y agoNo. Although that's mostly because the standard rustfmt is very generous about using vertical space.
- Filligree 8y agoFor high-level code it usually does. Idiomatic Rust can look surprisingly similar to Python. That doesn't apply to driver development, but it might be surprising to someone only moderately familiar with the language.
- klohto 8y agoI’m not sure if this is a critique or a question. If former, then I don’t understand why would that matter. Even at 4 times the length of the code, Rust is safer, more robust and better optimized. So why would the length matter?
- Riverheart 8y agoOne of Rust's selling points is zero cost abstractions which would normally imply doing more work with less code (as you would expect in a high level language like Python). So it is surprising that it takes that many more lines of code to achieve what C does without those abstractions.
- rantanplan 8y ago"zero cost abstractions" usually means you don't pay a performance penalty for abstractions you do not use.
- tjoff 8y agoAlso means that the abstractions themselves don't introduce unnecessary overhead. It's taken from C++. C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for [Stroustrup, 1994]. And further: What you do use, you couldn’t hand code any better. – Stroustrup https://blog.rust-lang.org/2015/05/11/traits.html https://blog.rust-lang.org/2015/05/11/traits.html
- coldtea 8y ago1300 vs 1000 is not "that many more". It's close to insignificant to comparing two languages. Especially when they boil down to things like: let ptr = unsafe { libc::mmap( ptr::null_mut(), len, libc::PROT_READ | libc::PROT_WRITE, libc::MAP_SHARED, file.as_raw_fd(), 0, ) as *mut u8 }; vs calling something like this in one line in C, etc. The C version also crams every line together, the Rust version couples related statements but leave newlines between others (not sure if loc counts those too).
- todd8 8y agoThis might be a preference that isn’t universally shared, but I really do find shorter programs easier to read. I don’t mean code-golf style short, but languages that don’t need a lot of boilerplate or ceremony. I’ve frequently used Python on personal projects for over 20 years because of its expressiveness. Of course, correctness trumps almost everything in programming. Rust seems like a really nicely balanced programming language that can replace C++ and C and provide additional protection against some kinds of common errors made by programmers. So it’s definitely on my list of languages to try next for a project.
- coldtea 8y ago>This might be a preference that isn’t universally shared, but I really do find shorter programs easier to read. Even so, 1300 vs 1000, or 1.3x is not really in the "so much more as to be more difficult to read" category. In fact it can be in the "better worded and easier to read category" (e.g. a monolithic stream of code without functions and comments vs one with).
- todd8 8y agoYou’re right. Ada has come up in this thread and there was a time when I was excited about Ada (I actually worked in a group that was competing in the early Strawman—Ironman proposals that led to the design of Ada), but Ada really is too verbose for my taste. Rust might end up being just right, not too concise, like Haskell or APL, and not too verbose, like Java or Ada; it could be a Goldilocks language.
- otikik 8y agoThat isn't surprising, really. Rust can go low-level, but its succintness (compared to C) is when dealing with high-level structures. Things like vectors, hashmaps, or even strings. Those rarely are use on a driver. In addition to that: in C you can share a pointer in many places and write memory wherever you want. Since rust has ownership system things get a little more complicated. Sometimes you have to write extra code to appease the borrow checker. Consider that you will never get the situations you get in C, where forgetting to check for NULL in one place can result in privilege escalations (example: https://www.exploit-db.com/exploits/33322/ https://www.exploit-db.com/exploits/33322/). I think it's worth it.