35 comments

[ 2.9 ms ] story [ 83.0 ms ] thread

  let s1 ="hello world";
  let s2 = s1; // copied – x1 is still valid
  let x1 = 2;
  let x2 = x1; // copied – x1 is still valid

  println!("{} for the {}nd time!", s1, x1);
Typo in first comment? s1 vs x1?
Example for skipping return keyword in case of early return is just wrong. It can be made to work if you have if x > 0 { x } else { x + 1 } but then it isn't early return anymore.
Thank you. We fixed this unfortunate example, and added some more explanation.
Err Too many redirects
we had some trouble but now it’s working
This seems like a fairly gentle introduction. I will definitely consider it as a starting point for more junior developers interested in touching our Rust code.

For those with more experience, Rust By Example walks through the same concepts in a more compact form that also lets you experiment with the code in your browser: https://doc.rust-lang.org/rust-by-example/index.html

Skimming through this, there are syntax errors (e.g. → instead of -> and ’ instead of ' in some function signatures), inconsistencies in code style, and a number of content errors (e.g. the content surrounding early return is still quite wrong and suggests a lack of understanding of the actual feature, expression-orientation; and the description of what &str is; and the description of Copy and copy semantics being simplified beyond what I would consider truth). Overall it’s a decent summary and most of the content seems correct, but be careful.
The arrow thing is probably just editor font ligatures.
(comment deleted)
I'm increasingly intrigued by Rust. Can anyone recommend a good analysis / benchmarking of Rust compared to C/C++ code? Curious how it fares on a performance basis.
This is by no means scientific, but it’s a fun benchmark, there’s some debate about the fastest Rust options submitted (ie I believe there were faster than C versions that were rejected).

C: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

Clang (for common llvm backend between rust and c): https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

C++: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

Rust is pretty much on par with both C and C++.

> This is by no means scientific,

It's actually very scientific (testing hypothesis, measuring results, etc) but might not be generalizable.

What's missing in these tests is that they do not capture the required knowledge by the developer to achieve those results.

If anybody has actually checked C/C++ examples, one could see that they use non-trivial techniques/libraries to achieve superior performance.

What would be REALLY interesting to see is breakdown of the performance by the years of experience of people that submitted working code examples.

I have a feeling that a developer with one year of experience in Java, will probably beat developers with two years of experience in C/C++.

Do you have an example of program performance measurements that do "capture the required knowledge by the developer" ?

How can you tell that the knowledge was actually "required" rather than incidental ?

Wouldn't it depend exactly what experience they gained in that one year and how relevant it was to the task at hand?

> I believe there were faster than C versions that were rejected

Believe but do not know? Shade?

On par is an understatement. It beats C++ in 8 benchmarks and loses in 2. Quite impressive.

Rust has more potential for better optimisation than C++ because the compiler has full information on pointer aliasing.

Always look at the source code (or even the CPU load).

One of those C++ comparisons is currently multi-core vs sequential.

Several C++ programs were recently removed, which upends things. You might say — "there were faster than [Rust] versions that were rejected".

One is multicore, but the other 7 winning have the same parallelism. As for that one winning by parallelism - maybe it was just easier to provide a correct parallel solution in Rust than in C++? This game didn't appear there yesterday and C++ developers had much more time to play it. If that's the case, that benchmark still counts.
Maybe it was just that I contacted the Rust programmer and suggested they might like to update their program :-)

(The changes were in the last month or two.)

You can expect it to be in the same ballpark as C or C++ for single-threaded code, especially if you use idiomatic Rust (combinators instead of manual iteration).

Where Rust shines is that it often takes jaut a few minutes to turn a single-threaded program into a multithreaded program — and the type system guarantees that the program won't have data races.

Technically it's about the same speed, but idiomatic Rust programs are structured differently, which has its ups and downs.

I wrote an overview of Rust's performance characteristics: https://kornel.ski/rust-c-speed

If you are more of an interactive learner I would recommend installing rust on your machine and then cloning the rustlings repo https://github.com/rust-lang/rustlings.

Rustlings is an interactive collection of rust exercises that are either incorrect or incomplete. The exercises are grouped into key concepts of rust and cross referenced with relevant chapters in the rust book https://doc.rust-lang.org/book/

The general flow is to open up the given chapter of the rust book for a set of exercises and hack away at them until they compile. It's a good way to quickly get an introduction to rust and its core features.

Having gone through rustlings while learning rust, I can say that apart syntax overview, they are almost useless.

I was much more successful by studying the rust book while trying to solve/implement not completely trivial pet projects.

Do you mind sharing what kind of projects? Just in case you have a list laying around.
That would be something like:

* ToDo list with UI

* Stock Quote retrieval/analysis

* Toy VM/Interpreter

"Too many linkedlists" is an excellent tutorial for understanding ownership IMO.