0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust言語の勉強を初めてみた。その3

Posted at

Data Type

Data Type

スカラーと複合の2つのタイプがある。
Rustでは、コンパイル時に変数の型を知っている必要がある。
通常は自動で認識するが、文字を解析して数値型に変換する場合などは、型注釈を付ける必要がある。

let guess: u32 = "42".parse().expect("Not a number!");

型注釈がないとエラーになる。

Scalar Types

Integer Types

Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
arch isize usize

archはコンピュータのアーキテクチャによる
64bitアーキテクチャの場合は64、32bitアーキテクチャの場合は32

Number literals Example
10進数 98_222
16進数 0xff
8進数 0o77
2進数 0b1111_0000
バイト(u8のみ) b'A'

'_'は視覚的な区切り記号。金額表示に使用する'$1,000'の','のようなもの。

Floating-Point Types

f32とf64の2種類がある。それぞれビットサイズが32bitと64bit。デフォルトは64bit。

fn main() {
    let x = 2.0; // f64

    let y: f32 = 3.0; // f32
}

Numeric Operations

基本的な四則演算が可能。

こちらに全ての演算リストがある。

fn main() {
    // addition
    let sum = 5 + 10;

    // subtraction
    let difference = 95.5 - 4.3;

    // multiplication
    let product = 4 * 30;

    // division
    let quotient = 56.7 / 32.2;

    // remainder
    let remainder = 43 % 5;
}

The Boolean Type

trueとfalseを取るbool型がある。サイズは1バイト

fn main() {
    let t = true;

    let f: bool = false; // with explicit type annotation
}

サンプルではfalse時に型を指定しているが、無くても問題無かった。

The Character Type

シングルクォーテーションで文字を使用することが出来る。
文字列はダブルクォーテーションであることに注意。

fn main() {
    let c = 'z';
    let z = 'ℤ';
    let heart_eyed_cat = '😻';
}

Compound Types

Rustには、タプルと配列の2つがある。

The Tuple Type

様々なタイプの値を1つにグループ化する。サイズは固定長で拡大縮小することはできない。

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
}

タプルから値を取り出すときは、以下の様に分解する。

fn main() {
    let tup = (500, 6.4, 1);

    let (x, y, z) = tup;

    println!("The value of y is: {}", y);
}

ピリオドを付けて値のインデックスを指定することで、直接アクセスすることも出来る。

fn main() {
    let x: (i32, f64, u8) = (500, 6.4, 1);

    let five_hundred = x.0;

    let six_point_four = x.1;

    let one = x.2;
}

The Array Type

配列は全ての要素が同じ型でなければならない。固定長。

fn main() {
    let a = [1, 2, 3, 4, 5];
}

柔軟に使用するにはベクトルを使用した方が良い。

以下の様に型と要素数を記述することが出来る。

let a: [i32; 5] = [1, 2, 3, 4, 5];

以下の様に書くことで、

let a = [3; 5];

let a = [3, 3, 3, 3, 3];とすることも出来る。

Accessing Array Elements

以下の様にして、要素にアクセスすることができる。

fn main() {
    let a = [1, 2, 3, 4, 5];

    let first = a[0];
    let second = a[1];
}

firstにはインデックス0の1が入る。
secondにはインデックス1の2が入る。

Invalid Array Element Access

fn main() {
    let a = [1, 2, 3, 4, 5];
    let index = 10;

    let element = a[index];

    println!("The value of element is: {}", element);
}

上のように配列の長さより大きなインデックスを指定すると実行時にエラーになる。

thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src\main.rs:5:19

低水準の言語ならば無効なメモリにアクセス出来てしまうが、Rustではエラーを検出してすぐに終了する。
こうすることによりこの種のエラーからユーザーを保護する。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?