Rust

Rust 제어문

hellowhales 2024. 7. 20. 10:49
728x90

https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html

 

Variables and Mutability - The Rust Programming Language

As mentioned in the “Storing Values with Variables” section, by default, variables are immutable. This is one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers. However, y

doc.rust-lang.org

 

fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

 

Rust의 제어문은 다른 언어와 특별히 다르지 않습니다. C/C++과 같게 if/else문을 쓰네요.

 

fn main() {
    let number = 6;

    if number % 4 == 0 {
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else if number % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}
728x90