Need some help with if/match in rust

Not sure if there are people with some rust knowledge arround, but i could use some help comparing Arguments to strings.

The relevant code:

use std::env;
let args: Vec<_> = env::args().collect();

let check = &args[1];
if check.trim() == "CPU" {
    let sys_descr = "CPU"
} else {
    let sys_descr = "RAM"
}

The strings are just placeholders for now. I’m trying to take an Argument, check if this Argument is either “CPU” or “RAM” and act upon it.

The Code runs, but the if statement isn’t executed. Nothing get’s assigned to sys_descr. I also get no errors from this.
What am i doing wrong here?

Ideally, i think, i’d like to use a match statement here. It keeps telling me though, that let isn’t supported in that place. So, how can i make this work?

Edit:
Additional question: Does Rust support some kind of “Named Arguments”? So, can i have my Code accept something like -t --name -H?

If I had a guess it is executed and you are looking at the sys_descr after the if block. Witch if rust has scopes like normal programming languages do you would have to define your variable outside the if or its only going to be available in the if.

Meaning put let sys_descr above the if and remove the let’s within the if.

You can also do it as described here
https://doc.rust-lang.org/reference/expressions/if-expr.html
Guess now I know where kotlin stole that feature from. :wink:

let sys_descr = if check.trim() == "CPU" {
    "CPU"
} else {
    "RAM"
}
4 Likes

Was about to suggest this. The variable scope is only defined in that if block.

Great. I’ll test this tomorrow!

So, Update.
I worked through a LOT of documentation and started to get a better grasp of how Rust works. Since this is my first actual Production Project, i’m just getting used to it.

  1. I’ve ditched all the manual Argument management for StructOpt. This allows for automated management of Arguments with proper long and short handles, and it creates a help page for you.
  2. What wasn’t clear from my condensed Code above was, that i’m storing an Array in sys_descr. I learned that Arrays are always fixed size. Since my Data is variable in length, i’m now assining what i need into seperate Varables beforehand.
  3. If you reassign Variables like that you have to do it differently. You need to define the Variable before the if loop and make it explicitly mutable with let mut var = value. In the ifloop you then leave out the let keyword

So yeah, all that is working now. I don’t like assigning a bunch of Variables that i’ll never use, but for the overall scope of this Project, it shouldn’t matter too much.

Now on to learning how functions take values and Return Values work in Rust.

You can also use Lists for that.

https://doc.rust-lang.org/std/collections/index.html

For some reason rust calls a simple list Vec :thinking:

That way you dont have to create a new array when the size changes (it can grow or shrink without much effort). Its basically just some ‘GodObject’ (list or I suppose Vec) and that points to a ‘ListObject’ and that ‘ListObject’ points to the next ‘ListObject’ until next is null/unset. And you can loop threw that with for my_list_item in my_list { .... }. Doing a loop with indexes would be a bad idea since when you just tell the Vec you want item with index 50 it would have to loop threw 50 ListObjects to get it for you. While that kind of for loop does not need to do that.