if #2

https://doc.rust-lang.org/book/if.html

3項演算子的な使い方ができる。 println!の引数に使えるかを試してみたところ普通に動いた。

fn main() {
    let y = if x == 5 {
        10
    } else {
        15
    }; // y: i32

    println!("y is {}", y);

    println!("? is {}", if x == 5 { 10 } else { 15 });
}
$ cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/4_5_if`
y is 10
? is 10

if

https://doc.rust-lang.org/book/if.html

カッコが無くても問題ない。

fn main() {
    let x = 5;

    if x == 5 {
        println!("x is five!");
    }
    if (x == 5) {
        println!("x is five!");
    }
}

逆にカッコをつけると警告が・・・

$ cargo run
warning: unnecessary parentheses around `if` condition, #[warn(unused_parens)] on by default
 --> src/main.rs:7:8
  |
7 |     if (x == 5) {
  |        ^^^^^^^^

x is five!
x is five!

Comments

Commentsrustdocというツールでコメントからドキュメント生成できるらしい。Markdownで記載できるらしい。 ブロックコメントについて説明は無かったけども、/* comment */は普通にコメントになった。

//! # The Rust Standard Library
//!
//! The Rust Standard Library provides the essential runtime
//! functionality for building portable Rust software.

fn main() {
    // println!("01 Hello, world!");
    /// println!("02 Hello, world!");
    // println!("03 Hello, world!");
    /* println!("04 Hello, world!");
    println!("05 Hello, world!"); */
    println!("06 Hello, world!");
    println!("07 Hello, world!");
}

Functions

Functionsを読む。

return x + 1;x + 1(セミコロンがない)が同じ扱いなのが面白い。 あと、プロトタイプ宣言不要なのがC言語から入った身としては嬉しい。

fn main() {
    let f: fn(i32) -> i32; // 関数ポインタ

    f = add_one;

    print_number(5);
    print_sum(5, 6);
    print_number(f(5));
    diverges();
}

fn print_number(x: i32) {
    println!("x is: {}", x);
}

fn print_sum(x: i32, y: i32) {
    println!("sum is: {}", x + y);
}

fn add_one(x: i32) -> i32 {
    x + 1
}

fn diverges() -> ! {
    panic!("This function never returns!");
}
$ RUST_BACKTRACE=1 cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/4_2_functions`
x is: 5
sum is: 11
x is: 6
thread 'main' panicked at 'This function never returns!', src/main.rs:26
stack backtrace:
   1:        0x10270193a - std::sys::imp::backtrace::tracing::imp::write::h917062bce4ff48c3
   2:        0x1027036cf - std::panicking::default_hook::{{closure}}::h0bacac31b5ed1870
   3:        0x10270296f - std::panicking::default_hook::h5897799da33ece67
   4:        0x102702ee6 - std::panicking::rust_panic_with_hook::h109e116a3a861224
   5:        0x1026fac93 - std::panicking::begin_panic::h634e2b37a96f78d4
   6:        0x1026fb152 - _4_2_functions::diverges::h68ff7cf679053ab8
   7:        0x1026faf43 - _4_2_functions::main::h67a617938b3bde74
   8:        0x102703c8a - __rust_maybe_catch_panic
   9:        0x102703156 - std::rt::lang_start::hd661476ce2fc2931
  10:        0x1026fb189 - main

Variable Bindings

Variable Bindingsを読む。

幾つか面白い動きがあった。デフォルトが書き換え不可(immutable)だからできる動きなんだろうな。

fn main() {
    /* スコープ:{} 以下でのみ有効 */
    let x: i32 = 17;
    {
        let y2: i32 = 3;
        println!("The value of x is {} and value of y2 is {}", x, y2);
    }
    // println!("The value of x is {} and value of y2 is {}", x, y2); // This won't work

    /* スコープ:同じ変数名での動き。 */
    let x2: i32 = 8;
    {
        println!("{}", x2); // Prints "8"
        let x2 = 12;
        println!("{}", x2); // Prints "12"
    }
    println!("{}", x2); // Prints "8"
    let x2 =  42;
    println!("{}", x2); // Prints "42"

    /* 型のある言語だけど、同じ名前で色々と代入できる。 */
    let y = 4;
    println!("{}", y);
    let y = "I can also be bound to text!"; // y is now of a different type
    println!("{}", y);
}