Variable code first to programmee

fn main() 
{
    let  mut x=10;
    let y=6;
    println!("x={} and y={} are two numbers",x,y);
    x=x+y;
    println!(" The result value is {}",x);
}

(Playground)

I see that the program outputs the result of 16, but normally, you would only want to post code to users.rust-lang.org if you wanted to ask a question, solicit code review, or invite people to use it.

So, do you need help with something?

4 Likes

NO, I JUST WANTED TO POST IT FOR U GUYS TO SEE IT

alright, then...

welcome to this forum, and have fun on your Rust journey :wink:

By the way, feel free to try out the “Rustfmt” tool (under “Tools” in the playground, or with cargo fmt if you're working in a cargo project in your computer) to make your code look nicer, and more readable for experienced Rust users.

Also, did you know that there's a += operator, so you can write x = x + y; shorter as x += y;?

Also, you might like the (still relatively new) feature that you can put the variable name directly in the {}s in a println; something like so:

fn main() {
    let mut x = 10;
    let y = 6;
    println!("x={x} and y={y} are two numbers");
    x += y;
    println!(" The result value is {x}");
}
1 Like

Great hint!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.