Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter3.1 変数と可変性 #5

Open
euledge opened this issue Feb 11, 2019 · 2 comments
Open

Chapter3.1 変数と可変性 #5

euledge opened this issue Feb 11, 2019 · 2 comments

Comments

@euledge
Copy link
Owner

euledge commented Feb 11, 2019

変数と可変性

  • 変数は標準の宣言では不変(immutable)となる
  • 変数を可変としたい場合は mut キーワードをつけて宣言する必要がある。
let mut x = 5;
  • 定数は const で宣言する
  • 定数には型を表す注釈 :u32 が必ず必要
  • 慣例として定数は大文字のスネークケースで表す
const MAX_POINTS: u32 = 100_000;

-数値は位をわかりやすくするためにアンダースコア _を入れることができる。

@euledge
Copy link
Owner Author

euledge commented Feb 11, 2019

3.1.2 シャドーイング

前に定義した変数と同じ名前の変数を再定義することができ、このことをシャドーイングという。

fn main() {
    let x = 5;
    let x = x + 1;
    let x = x * 2;
    println!("The value of x  is: {}", x);
}

上記のコードの結果は 12 を表示する。

fn main() {
    let mut x = 5;
    x = x + 1;
    x = x * 2;
    println!("The value of x  is: {}", x);
}

シャドーイングを使用しなくても mutを指定し変数を可変とすることで同じ結果の表示はできる。
ただし、 mut を使用したときは意図しない代入が可能になるが 明示的に letを用いることで値を変更する意図を表すことができる。

let spaces = "    ";
let spaces = spaces.len();

また、letは変数を新たに定義することなので上記のように、値の型を変更しつつ同じ変数名を使いまわすことが可能になる。

let mut spaces = "    ";
spaces = spaces.len();

同じことを mut を使用した場合はコンパイルエラーとなる。

$ cargo check
    Checking valiables v0.1.0 (D:\workspace\RustGuidebook\Chapter3\valiables)
error[E0308]: mismatched types
  --> src\main.rs:10:14
   |
10 |     spaces = spaces.len();
   |              ^^^^^^^^^^^^ expected &str, found usize
   |
   = note: expected type `&str`
              found type `usize`

error: aborting due to previous error

@euledge
Copy link
Owner Author

euledge commented Feb 11, 2019

3.2 データ型

通常は、式の右辺の型を推論して左辺の変数の型を決定してくれるが、右辺の取りうる型が複数存在する場合は、左辺の変数に型注釈をつけて宣言する必要がある。

暗黙の型変換は存在しないので以下のコードはコンパイルエラーとなる。

println!(" x is : {}", 5.0/4);
4 |     println!(" x is : {}", 5.0/4);
  |                               ^ no implementation for `{float} / {integer}`
  |
  = help: the trait `std::ops::Div<{integer}>` is not implemented for `{float}`

タプル

複数の値を一つの型にまとめることができる。要素ごとに異なる型を入れることもできる

fn main() {
    let tup = (500, 6.4, 1);
    let (x, y, z) = tup;
    println!("x: {} y: {} z: {}", x, y, z);
    println!("tup.0: {} tup.1: {} tup.2: {}", tup.0, tup.1, tup.2);
}

変数 tupの値は、個別の変数に分解 let (x,y,z) = tup; によって新たな変数に変換することで取り出すこともできるし、
添え字を用いて tup.0, tup.1, tup.2 のようにアクセスすることもできる。

配列

配列も複数の値を格納できる型になりますが、タプルと違って異なる型の要素を格納することはできません。また宣言時に決定したサイズで固定長です

fn main() {
    let array = [1, 2, 3];
    println!("array[0]: {} array[1]: {} array[2]: {}", array[0], array[1], array[2]);
}

@euledge euledge changed the title Chapter3 普遍的なプログラミング概念 Chapter3.1 変数と可変性 Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant