by VWWHFSfQ · 3y ago · 0 comments
Don't use String you fool, you want &str!
Don't use Vec you fool, use &[]!
It really puts people off. Because now they have to learn all about lifetimes and that's a fucking nightmare. My advice: Use String. Use Vec. Use Hashmaps. It's all still going to be a million time faster than whatever you were doing in Python, or Java, or Go, or Javascript, or whatever.
Tangent: I can't think of a single other language that's like this: the higher-level languages tend to pass by reference, and the lower-level languages tend to have you explicitly clone things.
With apologies to jwz.
Java has references only by design, so you can't do that.
Also if you are super sloppy with allocations I would expect something like Java or Go would be faster since GC is more efficient.
Because Rust excels on a lot of dimensions other than "systems programming"; Cargo is fantastic, the ecosystem of packages is great, it's one of the only languages that takes cross-platform support (including Windows) seriously, and it's got sum types.
Or, as withoutboats put it: https://twitter.com/withoutboats/status/1658536526043570179
Including some that run on the JVM and 30 years of Java, makes Maven Central certainly beat cargo.io in package count.
So there are options.
> Including some that run on the JVM
True. However, most that want to keep interoperability with Java, in order to get benefit of huge deposit of packages. That often means having to repeat Java's mistakes to maintain compatibility.
This is just programming culture; given two ways of doing something, with one being harder but more performant, we gravitate to the harder one at all costs.
In existing codebases I’ve worked with, often 90% or more boxed primitives are unnecessary, introduce unnecessary errors (assumed non-null refs vs. guaranteed non-null values) and performance penalties. They have a purpose, but there are libraries that address the first concern and Optional${Type} that are more expressive for the second.
I'd discount this part entirely. Using a boxes/non-primitive type for nullability is likely a design mistake. I can go in a full rant mode, in practical terms I tend to reject pull requests that do that.
Especially at DB boundaries, there’s decisions to make. If you put yourself in a position of your db having entirely non-nullable fields, you end up in one of two situations:
1) your whole db is key/value
2) you have magic values
Making all key/value doesn’t solve the nullability issue at the code boundary if you’re representing as an object, so you’re left with magic values. I think we all know how nice magic values are.
Ideology is the enemy of practicality.
You can turn them off though, and most of the culture will support you in ignoring them if that's appropriate to your use case.
Not at all. Systems languages don't magically make all code faster, they just help you write fast code. It's not that hard to tank performance if you're careless with allocations. I've seen some really slow C++, written by people who liked to use new everywhere like it's Java.
If you want not to care about this stuff, I think that Java would fit your programming style better (or Kotlin, if you dislike Java's verbosity). The JVM is quite tolerant of doing lots of unnecessary allocations.
https://programming-language-benchmarks.vercel.app/rust-vs-j...
For giggles, also checkout the amount of generated machine code in godbolt with a simple Hello world that uses &str vs String. Its like 10x the instructions :P
I'd consider the entire site/process flawed.
If you do not want to learn about lifetimes do not use Rust. The advice is also terrible since inevitably you are going to use someone elses code, which does use slices.
Also, copies are not bad and they're often a better tradeoff than microoptimizing all your lifetimes.