Giter Club home page Giter Club logo

Comments (2)

euledge avatar euledge commented on August 17, 2024

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

from rustguide.

euledge avatar euledge commented on August 17, 2024

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]);
}

from rustguide.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.