【学習3日目】クレート

昨日のつづき。
Smentic versioning はじめて知った言葉。0.0.0みたいなやつ。
セマンティックバージョニング - Wikipedia

https://crates.io/
Crates.ioは、Rustのエコシステムにいる人たちがオープンソースのRustプロジェクトを投稿し、他の人が使えるようにする場所


% cargo build
Updating crates.io index
Downloaded ppv-lite86 v0.2.17
Downloaded rand_core v0.6.4
Downloaded rand_chacha v0.3.1
Downloaded rand v0.8.5
Downloaded libc v0.2.138
Downloaded getrandom v0.2.8
Downloaded cfg-if v1.0.0
Downloaded 7 crates (794.8 KB) in 1.31s
Compiling libc v0.2.138
Compiling cfg-if v1.0.0
Compiling ppv-lite86 v0.2.17
Compiling getrandom v0.2.8
Compiling rand_core v0.6.4
Compiling rand_chacha v0.3.1
Compiling rand v0.8.5
Compiling guessing_game v0.1.0 (/usr/home/fbsd/rust-codes/guessing_game)
Finished dev [unoptimized + debuginfo] target(s) in 4m 07s
分からない言葉「トレイト」
たぶん、クレートの中にあるもの?
→トレイトについて詳しくは第10章で解説します。

「スコープ」これもまだわからん。

use std::io; ←これは標準ライブラリ
use rand::Rng; ←これは rand クレートのrngトレイト???

今日は 「乱数を生成するまで」
明日は「予想と秘密の数字を比較する」

【学習2日目】数当てゲーム・入力部分まで

Wikipedia機械語、バイナリ、などの言葉を調べる。繋がりでいろいろなまだ分からない用語を調べる。わからないものはそのままにしておく。
そこでふと昨日Rustで作ったhello world の実行ファイルの大きさを知りたくなり、見てみたらなんと 4.1Mもある。たったこれだけでなんでこんなに大きいのかと思い、C言語で同じものを作ったら、それはたったの15kしかない。「rust 実行ファル 大きい」で検索して以下を読んだ。
Rustのバイナリが大きい理由 | κeenのHappy Hacκing Blog
Rustのバイナリサイズを削減する - Qiita
いまのところはなんとなく読んだ。いつかよく分かるようになる日がくるだろう。というわけでメモしとく。

今日は the book 2. 数当てゲームのプログラミングを学習する。

その前に昨日の復習
・rustc main.rs でコンパイル
・cargo new project-name で新しいカーゴのディレクトリを作れる
・そのディレクトリに入って、cargo build でコンパイルする。
・cargo run でコンパイル、実行までセットで行う
・cargo check でコンパイルできるかのチェックができる。
・実行ファイルは target/debug 内に実行ファイルが作られる。
・cargo build --release とすると target/release 内に実行ファイルが作られる。
・cargo new で出きたディレクトリの中にある内容物を一つずつ見て回る。

2. 数当てゲームのプログラミング をやってみる。
エディター(Vim)の使い方はこちらを見ながら、使いながら覚える。
よく使う Vim のコマンドまとめ - Qiita
Vimのコマンド一覧。最速のスピード入力を極めろ!

今日は 「最初の部分をテストする」まで終える。
明日は 「秘密の数字を生成する」から。

【学習1日目】これまでプログラミング言語を習得できなかった人がRustを習得できるかどうか

無論仕事としてプログラミングをしたことは無し。趣味でちょこちょこいじってどれも習得できずに中途半端。

下地として以下

HTML = なんとなく読み書き。CSS嫌い。現在の小難しいのは無理。

JavaScript = 必要上ちょっとやったが嫌い。二度と触りたくない。

Perl = 人から頼まれて既存スクリプトのカスタマイズしたくらい。

php = 人の仕事の手伝いで既存コードのカスタマイズしたくらい。

シェルスクリプト = 好き。でも自分で一からは書けない。いろいろパクって組み立てる。

C言語 = 昔、「プログラミング言語C 第2版 ANSI規格準拠 (カーニハン/リッチー)」で1章目から挫折。

プログラミング習得 = 自分には無理、と思う。最初に出てくる「Hello world」を見るだけで拒絶反応が…。

しかし、Rustの記事を見かけたとき、これは挑戦してみたいと思えた。というかいい加減なにか一つ習得したい。ハードウェア寄りが好きなので、組み込み系(というのかな?)小さなチップ上で小さな機械を制御するようなことができればいいかなと。家電制御に興味がある。

で、これまでの挫折の経験から、学習進捗をここに記録していこうということです。自分用。学習方法なども現在いろいろ検索中。そもそも習得することで自分に何が出来るかもまだ良くわからないが、(大それたことだが) それで仕事の足しになるのが最終目標か? まあ、夢は大きく。

まず以下の読みを開始。

doc.rust-jp.rs

マシンはFreeBSD。2022年12月11日現在、pkgにもportsにも rustup は無いようです。

以下でRustのインストールスクリプトをダウンロードして、まずは内容を読む。

curl -sSf https://sh.rustup.rs/ > rustup-init.sh
curl コマンドオプション

  • s = Silent or quiet mode. Do not show progress meter or error messages.
  • S = When used with -s, --silent, it makes curl show an error message if it fails.
  • f = (HTTP) Fail fast with no output at all on server errors. This is useful to enable scripts and users to better deal with failed attempts.

読むときの less コマンドの基本的な操作

  • 次のページ = <スペースキー>
  • 前のページ = b
  • 最初のページ(というか1行目) = g
  • 最後のページ = G
  • 検索 = / を押して検索する語をタイプして<リターン>、nで後方。Nで前方。

して見たところ、FreeBSD とあったので、このshでいいのかな。chmod +x してから実行。

info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup home directory, located at:
/root/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/root/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at:
/root/.cargo/bin
This path will then be added to your PATH environment variable by modifying the profile files located at:
/root/.profile
/root/.zshenv
You can uninstall at any time with rustup self uninstall and these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-freebsd
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is x86_64-unknown-freebsd
info: syncing channel updates for 'stable-x86_64-unknown-freebsd'
info: latest update on 2022-11-03, rust version 1.65.0 (897e37553 2022-11-02)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-freebsd'
stable-x86_64-unknown-freebsd installed - rustc 1.65.0 (897e37553 2022-11-02)

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"


rustc --version
rustc 1.65.0 (897e37553 2022-11-02)

Hello, world できました。