LoginSignup
2
0

More than 5 years have passed since last update.

Rust by Examples のお勉強 - Primitives

Last updated at Posted at 2018-04-01

char は Unicode codepoint なので 4 バイト

Unicode codepoint は最大で 21 ビットなので、4 バイトになるということだろうか?

tuple

tuple は異なる型の値を格納できる。こんな風に中身に bind するのも可能。

let tuple = (1, "hello", 4.5, true);
let (a, b, c, d) = tuple;

array と slice

array の型は [T; len]、slice の型は &[T]

slice は array からの borrow なので、& が必要。

fn print_slice(slice: &[i32]) {
    print!("[");
    for (i, x) in slice.iter().enumerate() {
        if i > 0 {
            print!(", ");
        }
        print!("{}", x);
    }
    println!("]");
}

fn main() {
    let a: [i32; 5] = [1, 2, 3, 4, 5];
    print_slice(&a);
    print_slice(&a[1..4]); // a[1, 4)
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0