4 ms·
> I think Rust is harder to learn, but once you grok it, I don't think it's harder to use, or at least to use correctly. It's hard to write correct C because th
by kelnos 2y ago
> I think Rust is harder to learn, but once you grok it, I don't think it's harder to use, or at least to use correctly. It's hard to write correct C because the standard tooling doesn't give you much help beyond `-Wall`.
When I say Rust is harder to use (even after learning it decently well), what I mean is that it's still easier to write a pile of C code and get it to compile than it is to write a pile of Rust code and get it to compile.
The important difference is that the easier-written C code will have a bunch of bugs in it than the Rust code will. I think that's what I mean when I say Rust is harder to use, but I'm more productive in it: I have to do so much less debugging when writing Rust, and writing and debugging C code is more difficult and takes up more time than writing the Rust code (and doing whatever less debugging is necessary there).
> Also, I highly recommend getting into the habit of running `cargo clippy` regularly. It's a wonderful tool for catching non-idiomatic code.
That's a great tip, and I usually forget to do so. On a couple of my personal projects, I have a CI step that fails the build if there are any clippy messages, but I don't use it for most of my personal projects. I do have a `cargo fmt --check` in my pre-commit hooks, but I should add clippy to that as well.
- sestep 2y agoIf you're using VS Code then you can add `"rust-analyzer.check.command": "clippy"` to your `settings.json`. I assume there's a similar setting for rust-analyzer in other editors.
- maleldil 2y agoNeovim: require("lspconfig").rust_analyzer.setup({ settings = { ["rust-analyzer"] = { checkOnSave = { command = "clippy", allFeatures = true, }, }, }, })
- pabs3 2y agoYou might want to reconsider use of rust-analyzer, it isn't safe to use on code you haven't written yourself. https://rust-analyzer.github.io/book/security.html https://rust-analyzer.github.io/book/security.html
- swiftcoder 2y ago> it isn't safe to use on code you haven't written yourself Neither is cargo (nor npm, nor any other package manager, for that matter). I'm not sure what value being that paranoid is buying you in the long run.
- pabs3 2y agoPackage managers are for running other people's code, I would not expect the same of static analysis tools, especially since they are of use while auditing other people's code before building/running it.
- swiftcoder 2y agoCargo's threat model here is identical to that of rust analyser. If you trust your dependency tree sufficiently to run `cargo build`, then you trust it sufficiently to run rust analyser.
- pabs3 2y agoConsidering Cargo has build scripts, which are designed to run arbitrary code, that doesn't appear to be correct. https://doc.rust-lang.org/cargo/reference/build-scripts.html https://doc.rust-lang.org/cargo/reference/build-scripts.html
- swiftcoder 2y agoRust analyser executes those exact same build scripts. This is the primary avenue for exploits in both pieces of software, and is called out explicitly in the page you originally linked.
- pabs3 2y agoYes, and that is my complaint.
- kstrauser 2y agoThat's a fair distinction. Basically, it's easier to write C that compiles than Rust that compiles, but it's harder to write correct C than correct Rust. Regarding Clippy, you can also crank it up with `cargo clippy -- -Wclippy::pedantic`. Some of the advice at that level gets a little suspect. Don't just blindly follow it. It offers some nice suggestions though, like: warning: long literal lacking separators --> src/main.rs:94:22 | 94 | if num > 1000000000000 { | ^^^^^^^^^^^^^ help: consider: `1_000_000_000_000` | that you don't get by default.
- maleldil 2y agoYou can also add #![warn(clippy::all, clippy::pedantic)] to your main.rs/lib.rs file to get those lints project-wide.
- jpc0 2y agoThe truly pedantic setting here would by complaining about the magic number. Why 1_000_000_000_000, what does that number mean. It is for free to: let my_special_thing = 1_000_000_000_000 since the compiler will just inline it. The readability problem was never the lack of separators, since that number might be the wrong number regardless.
- ohgr 2y agoI’d add that the Rust code and C code will probably have the same number of bugs. The C code will likely have some vulnerabilities on top of those. Rust doesn’t magically make the vast majority of bugs go away. Most of bugs are entirely portable!
- naasking 2y agoI'd say whether Rust helps you reduce bugs depends on how good you are at creating abstractions and/or encoding properties in the type system.
- codedokode 2y agoRust allows to provide more information about types (generic types, pointer usage) and checks it, while in C you have to rely on doc comments and checking the code manually. Or am I wrong and C allows to specify pointer nullability, pointer ownership and array bounds?
- ohgr 2y agoNone of those things feature in any problem I deal with on a daily basis, whatever language I use. So for example today I dealt with a synchronization issue. This turned out to not be a code bug but a human misunderstanding of a protocol specification saga, which was not possible to code into a type system of any sort. The day before was a constraint network specification error. In both cases the code was entirely irrelevant to the problem. Literally all I deal with are human problems. My point is Rust doesn't help with these at all, however clever you get. It is no different to C, but C will give you a superset of vulnerabilities on top of that. Fundamentally Rust solves no problems I have. Because the problems that matter are human ones. We are too obsessed with the microscopic problems of programming languages and type systems and not concentrating on making quality software which is far more than just "Rust makes all my problems go away" because it doesn't. It kills a small class of problems which aren't relevant to a lot of domains. (incidentally the problems above are implemented in a subset of c++)
- nicoburns 2y ago> it's still easier to write a pile of C code and get it to compile than it is to write a pile of Rust code and get it to compile. As someone who is more familiar with Rust than C: only if you grok the C build system(s). For me, getting C to build at all (esp. if I want to split it up into multiple files or use any kind of external library) is much more difficult than doing the same in Rust.
- pkolaczk 2y agoGetting something to compile is never the end-goal. It takes 0 effort to get Python compile.
- burnished 2y agoIt can feel like a real milestone though