Rust 기본 문법

Oct 04, 2018

변수

        let mut x = 5;
        const MAX_POINTS: u32 = 100_000;
        let x = 1; 
        let x = x+1;

데이터 타입

            let c = 'z';
        let tup: (i32, f64, u8) = (500, 6.4, 1);
        let (x, y, z) = tup;
        // 직접접근 
        tup.0, tup.1, tup.2
- 배열 : 모든요소가 같은 타입. [index] 를 통한 접근


함수

    // 타입 추론 없이
    // without type inference
    let f: fn(i32) -> i32 = plus_one;

    // with type inference
    // 타입 추론 사용
    let f = plus_one;

주석

제어문

    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");
    }
    let number = if condition {
       5
    } else {
       6
    };

반복문

    for number in (0..10){
    //
    }

    let a = [10, 20, 30, 40, 50]
    for element in a.iter() {
    //
    }
    while number != 0 {
        println!("{}!", number);
        number = number - 1;
    }

#dev  #rust  #syntax