Reading "The #Rust Programming Language" Book πŸ‘€

So far?
- Shadowing same-scope variables – interesting πŸ€”
- Default immutability of variables and also references passed to functions – I like it 😯
- Implicit return values – nah thanks. *writes explicit return statement* 🀨

The #Rust ownership chapter is an interesting one πŸ˜―πŸ€“
Ah mh πŸ€”
'->' are called arrows
'=>' on the other hand are just described as "...then the '=>' operator..." πŸ™ƒ

Oof, got past the Generics, Traits and Lifetimes chapter now @pixelspree πŸ‘€

I mean, it's an additional effort on coding, but makes garbage collection or manual (and error prone) destruction by the developer obsolete πŸ’πŸΌ

@pixelspree Ok, I'm through πŸ˜€

I'd summarize the chapter about #OOP something like: #Rust is object oriented, but with very limited inheritance to what most people would expect πŸ€”πŸ’πŸΌ

Let's do the final exercise project πŸ€“

That feeling, if you have prepared a few unittests and bit by bit expand your actual code and they all just pass out of the box 😌

Two weeks went by, and I think I'm pretty far on having a small program for german #TribalWars players ready πŸ‘€
Would be cool having some experienced #Rust #developer have a code review of this thing then, how well (or bad πŸ€ͺ ) did coding wise.

One last thing missing, after that I just have to do some doc-commenting, readme and CI-pipeline stuff before I can push it

@9Lukas5 I found also "lambda operator".
@tagon used directly in the Rust book itself or somewhere else? πŸ€”
@9Lukas5 How about lifetimes... :)
@pixelspree Details about them is coming a huge bit later in the thing πŸ‘€
Finished Chapter 4, going to 5 (structs) next. Details about lifetimes is in Ch 10 πŸ’πŸΌ
@9Lukas5 Viel Erfolg. Ich habe hin und wieder mit Rust rumgespielt, aber bin bisher damit gescheitert.
Vermutlich lag es auch daran, dass ich mit dem Projekt (Sp Dr S 60 Simulation πŸ˜…) auch in anderen Sprachen noch keinen endgΓΌltigen Durchbruch hatte und ich gleichzeitig versucht habe, das Ganze fΓΌr Arduino zu kompilieren. πŸ˜…
@iGEL Ooof, das hatt ich auch mal kurz überlegt (also nicht speziell mit Rust, sondern generell), einen Stw-Sim als alternative zu https://stellwerksim.de/ zu bauen, weil mir diese Java-Webstart irgendwie auf'n Keks geht 😬
@9Lukas5 Yeah it's very valuable, just a bit tricky to understand in the beginning. I still can't figure out lifetimes sometimes in my code. It gets complicated when you have thins that don't own their own data. So it's a lot easier if you can make structs own their data.
@9Lukas5 You get used to the return thing (I am now having issues with the opposite). That the last statement of a block is its return value is actually super cool. Allows you to sneak println for debugging into many places.

@9Lukas5 the return thing is done out of consistency with other expressions.

these are all expressions which evaluate to a value - even functions can be thought of this way (kinda)

let a = func();

let a = {
let b = 5;
b + 7
};
// a = 12

let a = if 5 > 1 {
8
} else {
0
};
// a = 8

let a = match Some(0) {
Some(_) => 6,
None => 9,
};
// a = 6

@laund I'm used to it, that this kind of expression is only done e.g. in Java Lambdas or similar in-function expressions, but not for the actual function return value πŸ˜…

Seems kind of strange, that just a missing semicolon on the last line makes a statement to an expression 🫒πŸ€ͺ

But yeah, it makes it consistent and most likely one will get used to it πŸ’πŸΌ

@9Lukas5

Regarding implicit return values:
Almost everything is an expression in Rust, so you could write:

let foo = if condition {
do_something();
bar
} else {
baz
};

so `foo` will be assigned the value `bar` or `baz`.

See also: https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html#ifelse

It becomes very natural to not use `return`. And believe me: you _will not_ get it wrong. The Rust compiler is very strict, so "accidentally" returning the wrong value won't happen.

if/else - Rust By Example

Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.

@janriemer Out of curiosity I tried using an explicit 'return' in such an inline conditional assignment.

But an explicit 'return' statement will exit the function it is in, no matter how deep down it is placed in loops or conditions πŸ€ͺ

@9Lukas5 Yes, explicit return are very good for early exiting a function, totally agree here.

If you want to dive in a little deeper:

Did you know that Rust has it's own operator for handling such control flow to make it more explicit!?  

Enum std::ops::ControlFlow

https://doc.rust-lang.org/std/ops/enum.ControlFlow.html

ControlFlow in std::ops - Rust

Used to tell an operation whether it should exit early or go on as usual.