What is the difference between ; and ,?

1 - how do i know that is an expression i should use ; and that is an argument i have to use , ?

2 - why is , used instead of ; is not 0 => println!("it is 0") an expression ?

3 - if statement has else and els if and we don't add , if is one arm ?

You read the documentation of the language. The Book introduces every language construct with examples demonstrating the syntax, and the Reference contains a formal grammar too.

Your 2nd and 3rd questions are incomprehensible. If you are having trouble expressing yourself in English, please seek help from a fluent speaker.

you are not forced in anyways to reply this let others answer it it is maybe not for you

Take a 5 min. break, then try to read your message and see if you understand it. Whenever you are asking for help in any online forum, it's expected that you are as clear as possible to make it easy for others to help you.

1 Like

I also don't completely understand your questions, but I will try to answer them nonetheless.

The semicolon ; terminates a statement. It discards the value of the previous expression. For example, you use the semicolon to end a let statement:

let age = 20;

The comma , separates items, be they function arguments, type parameters, collection elements or anything else that falls into this category. Some examples that come to mind:

fn sum(a: i32, b: i32) -> i32 {
   a + b
}
let array = [1, 2, 4, 3];
let result: Result<(), usize> = Err(420);

Apart from their visual similarity, these two sigils have nothing in common. If you are not sure which of them to use, think of what you want to achieve first and then look up the syntax as needed.

how do i know that is a statement and i have to add ; not , ?

The Rust Reference provides a detailed insight into what is and what isn't a statement. In short, it is a declaration like let, struct or fn or an expression whose result is then discarded, like x += 2 or vec.push(number).
Again, this has nothing to do with the comma , and if you need a more basic approach to this kind of knowledge, you should read The Book.

1 Like

It looks like match arms might be confusing you. Semicolons are used in most of the places you would expect from working with a language like C. I don't want to get too into detail about where exactly that is but the Rust reference is probably the best place to get complete answer to that question.

Match arms are the only place where , gets used in a position that is similar to where a semicolon might go if the expression were at the top level of a function or something, but it's important to note that the comma is part of the match grammar and not the grammar of expressions or statements more generally. The grammar for match expressions in the reference shows this.

Here's an example match block with some comments to hopefully clarify a bit how the commas fit into the match grammar.

match 12 {
    // Commas are mandatory if the expression is an "ExpressionWithoutBlock"
    0 => println!("zero"), 

    // commas at the end of match arms are optional if the expression is an "ExpressionWithBlock"
    1 => {
        // Semicolons work normally inside the block. Technically you don't need one here because `println!` can be treated as 
        // an expression returning the unit type (sometimes informally called "void"), 
        // but that's not strictly relevant to your question.
        println!("one"); 
    },
    2 => {
        println!("two");
    } 

    // The comma on the final match arm is always optional
    _ => println!("Some other number") 
}
7 Likes

This may help. I know it's JavaScript, but semicolon/comma use is pretty universal.

Semicolons are used to split stuff into arrays or lists. As noted by @semicoleon, 0 => println!("it is 0") is part of a match statement, which is basically giving the computer a list of functions. Yes, println!("it is 0") is a statement, but it is inside a function, and since there are no braces, a semicolon is implied. The comma is separating the function, not the statement inside of it.
I have no Idea what you mean by " if statement has else and els if and we don't add , if is one arm ?" Can you explain or give an example?

Someone please correct me if I got something wrong, I am pretty new to rust.

No, it is not implied. If you don't use a semicolon in a match statement, it will evaluate to the expression, causing a type mismatch if the types are different for different arms. It doesn't matter here because println returns () anyways, but in case of other expressions, the difference is significant.

4 Likes

Oh, so a semicolon will cause it to return an empty value? Then it is just not needed and is never implied. Right?