- 追加された行はこの色です。
- 削除された行はこの色です。
- Rustの基礎 へ行く。
#author("2025-01-12T10:51:12+09:00","","")
#author("2025-01-12T10:51:32+09:00","","")
#setlinebreak(on);
#html(){{
<div style="position: relative;">
<div style="display: inline-block; border: 1px solid #ccc; color: #666; border-radius: 4px; position: absolute; top: -30px; right: 0; padding: 0 10px;">
作成: 2024-09-24、更新: 2024-09-24
作成: 2024-09-19、更新: 2024-09-19
</div>
</div>
}}
* 目次 [#ec37fe29]
#contents
- 関連
-- [[Rust]]
-- [[Rustのインストール]]
* rustc によるビルド [#c5243066]
#html(<div class="pl10">)
サンプル
#mycode2(){{
fn main() {
println!("Hello world!");
}
}}
ビルド/実行
#myterm2(){{
rustc main.rs. && ./main
Hello world!
}}
#html(</div>)
* cargoの使用 [#z9de07e0]
#html(<div class="pl10">)
https://doc.rust-lang.org/cargo/index.html
| コマンド | 説明 | 使用例/補足など |h
| cargo new プロジェクト名 | 新規パッケージを作成する | cargo new --bin sampleapp|
|~|~| cargo new --lib samplelib |
| cargo init | 既存ディレクトリに新規パッケージを作成する | |
| cargo build | プロジェクトをビルドする | targetディレクトリ配下に 実行可能ファイルが出力される |
| cargo run | アプリケーションを実行する | |
| cargo check | アプリケーションがビルド可能かどうかをチェックする | 実行可能ファイルは出力しない |
| cargo add | 依存ライブラリを追加する | |
| cargo remove | 依存ライブラリを削除する | |
| cargo clean | | |
| cargo doc | | |
| cargo test | | |
| cargo update | | |
| cargo publish | | |
| cargo install | | |
| cargo uninstall | | |
#html(</div>)
* データ型 [#l61fe581]
#html(<div class="pl10">)
https://doc.rust-lang.org/book/ch03-02-data-types.html
- 整数型
-- i8, i16, i32, i64, i128, isize
- 整数型(符号なし)
-- u8, u16, u32, u64, u128, usize
- 浮動小数点
-- f32, f64
- 真偽値
-- bool
- 文字
-- char (4バイト)
- その他
-- タプル、配列など(後述)
各数値型の格納可能なサイズはデータ型の数値部分と同じ(ビット)
isize, usize についてはコンピュータのアーキテクチャによって異なり、64 ビットマシンの場合は64ビット、32ビットマシンの場合は 32ビットとなる。
#html(</div>)
* 基本構文 [#e5ed75b8]
#html(<div class="pl10">)
** 変数の宣言と代入 [#of3b2b52]
#html(<div class="pl10">)
変数は let で宣言を行うが、デフォルトでこの変数はイミュータブル(変更不可)となる。
#mycode2(){{
let num1 = 10;
println!("num1: {}", num1);
}}
可変(ミュータブル)にするには宣言に mut を付与する。
#mycode2(){{
let mut num1 = 10;
println!("num1: {}", num1);
num1 = 20;
println!("num1: {}", num1);
}}
結果
#myterm2(){{
num1: 10
num1: 20
}}
変数は再宣言する事ができる。(シャドーイング)
以下のコードは前のコードと同じ結果を出力する。
https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
#mycode2(){{
let num1 = 10;
println!("num1: {}", num1);
let num1 = 20;
println!("num1: {}", num1);
}}
使用を誤るとバグの温床にもなりかねない機能だが、
イミュータブルに保ちたい変数を任意のブロック内で利用する際に、前の変数と同じ名前で新しい変数を宣言する事が出来る。等の利点がある。
#mycode2(){{
fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {x}"); // 12
}
println!("The value of x is: {x}"); // 6
}
}}
#html(</div>)
** 関数 [#g688e517]
#html(<div class="pl10">)
#TODO
#html(</div>)
** 制御構文 [#i203d18b]
#html(<div class="pl10">)
#TODO
#html(</div>)
** 範囲 [#xc9b54f8]
#html(<div class="pl10">)
#TODO
#html(</div>)
** 配列、スライス、Vector [#k1f88450]
#html(<div class="pl10">)
#TODO
#html(</div>)
** トレイト [#zda7baa8]
#html(<div class="pl10">)
#TODO
#html(</div>)
** 所有権 [#me3a5838]
#html(<div class="pl10">)
#TODO
#html(</div>)
** 借用とClone [#e1135b9a]
#html(<div class="pl10">)
#TODO
#html(</div>)
** メモリの割り当てについて [#c6a8113e]
#html(<div class="pl10">)
#TODO
#html(</div>)
** パッケージ、クレート、モジュール [#f73df1f2]
#html(<div class="pl10">)
#TODO
#html(</div>)
#html(</div>)