何が違うの?
参考
- https://doc.rust-jp.rs/book/second-edition/ch03-01-variables-and-mutability.html
- https://doc.rust-jp.rs/the-rust-programming-language-ja/1.6/book/const-and-static.html
疑問: immutableとconstって何が違うの?
let x = 0;
でimmutable束縛const x: i32 = 0;
でconst定数
何が違うの?
解答
const
は型を明示する必要があるconst
はメモリアドレスが不定
アドレス参照しなければほぼ同じか。
let
, const
どちらを使うべき?
fn main() { let x = 0; const y: i32 = 0; }
わからん。ググっても情報ない。型を書かずに済むlet
が楽。
対象環境
- Raspbierry pi 3 Model B+
- Raspbian stretch 9.0 2018-11-13
- bash 4.4.12(1)-release
- rustc 1.34.2 (6c2484dc3 2019-05-13)
- cargo 1.34.0 (6789d8a0a 2019-04-01)
$ uname -a Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux
前回まで
- Rustを学んでみたい(プログラミング言語)
- Rustの環境構築
- RustでHelloWorld
- Rustの和訳ドキュメント
- Cargoでプロジェクト作成・ビルド・実行
- クレートとは?
- Rustで関数を使ってみる
- Rustでモジュールを使ってみる
- Rustで乱数を生成する(rand)
- Rustで標準入力する(std::io::stdin().read_line())
- RustでMatch判定する(match)
- Rustでprintとread_lineを1行にする方法
- Rustで数当てゲーム
- クレート名にドット.が使えない
- Rustの変数と可変性(let, mut) error[E0384]: cannot assign twice to immutable variable
x