1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「Rust と webAssembly によるゲーム開発」で Rust を勉強する地獄を楽しむ(1)

1
Posted at

「Rust と webAssembly によるゲーム開発」で Rust を勉強する地獄を楽しむ(1)

2025/12/20 から書き始めました。この本の解説ではなく、Rust 初心者のわたしがこの本で説明されていない Rust の技術を理解して書き溜めるのが目的です。

ここまで2度の挫折

購入したのはもはや半年前。本屋でうろうろして「Rust」と「ゲーム」という単語に惹かれて手を取りました。AIがすべてをコーディングする今の御時世。新しい言語を勉強する意味がどの程度あるか疑問でしたが、買ってよかったと思ってます。

普段キャラクタ端末で事足りるような仕事をしているので、やっぱり自分で書いて絵が出るというのは楽しくワクワクしながら進められました。でも途中で仕事が忙しくなり、三角形を描いたところで断念。これが1度目の挫折でした。

4ヶ月ほど経過して仕事も落ち着き、放置していたこの本をふたたび手に取りました。ほとんど忘れてしまっていましたが、Red Hat Boy が走る(ただし移動はしない)姿に喜び、その次のリファクタリングまで進みました。

しかし、2025年現在では著者の想定されてるバージョンが古すぎて大量の deprecate エラーや、新しく置き換わったものが多く、対応が難しくなっていきます。さらには、著者が変更部分をちょこちょこと出してくるおかげで、どこをどう変えたら良いかわからなくなる場面がしばしば。最終的に github からソースをダウンロードしましたが、こちらも依存関係が古くて動かすこともできず。本気でこれはだめだな、と古本屋にいつ行こうか思案していました。

Linux カーネルと Rust と再出発

ほこりをかぶってツルツルだった表紙がざらざらしてきたころ、たまたま Linux カーネルの experimental な Rust 導入が完了したというニュース二遭遇します。

変な話ですが、ああ需要あるんだみたいな実感を感じてしまいます。勉強してみようか、そういえばすでにうちにもう教本があるじゃないか、というわけで再度手に取ったわけです。

次こそは挫折しないために、以下のルールを決めました。これでなんとか完走したい。

  • Rust の基礎を同時に学ぶ
  • まとめとして記録を残す(この記事)
  • 最新の Rust に合わせて書き直す

Chapter 1. Hello WebAssembly

rust-analyzer が便利だよとあるがなにかわからないので調査。

rust-analyzer

rustup のインストールで一緒に入ったっぽいですが実行するとエラーになりました。

% rust-analyzer
error: Unknown binary 'rust-analyzer' in official toolchain 'stable-aarch64-apple-darwin'.
% file rust-analyzer
rust-analyzer: Mach-O 64-bit executable arm64

インストールに問題があるわけでないので、本にある通り vscode で確認することが適切のようです。
公式のチュートリアルを参考に main.rs を作ってエディタで開いてみます。

rust-anlalyzer-test % cat > main.rs
fn main() {
    println!("Hello, world!");
}
rust-anlalyzer-test % code .

開くと下のステータスバーで rust-analyzer が赤くなっていました。マウスホバーでエラーが確認できます。

Failed to discover workspace. Consider adding the Cargo.toml of the workspace to the 
linkedProjects setting.

プロジェクトを作る必要がありました。Hello, Cargo!を参考にやり直し。

% cargo new hello
    Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
% cd cargo
% cat Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]
% mv ../main.rs src/
% code .

なるほど。変更できるかもしれませんが、rust-anlyzer は vscode プロジェクトのトップディレクトリから Cargo.toml を読むので再起動が必要でした。これで rust-anlyzer が動くことを確認できました。この main.rs ではなんのありがたみもありませんが・・

rust-webpack → 終わってた

rust-webpack を使ってさくっとプロジェクトツリーを作る手順が記載されてましたが、これが今年の8月にアーカイブされてました。プロジェクトの作成は、私がまごまごしている間に終わってしまったようです。こちらを試してみます

手順をおおまかにまとめると。

  • wasm-pack をインストール
  • Cargo プロジェクトを作成して wasm-bindgen を追加
  • wasm-pack build --target bundler でコンパイル
  • プロジェクト内に site ディレクトリを作って npm プロジェクトにする
  • site 内に index.html など作成、npm run serve でブラウザからアクセス
  • あとは、wasm-pack build を実行すると site 側も更新される!

あとで参考になりそうな情報をメモ。JavaScript の関数を呼びたいときは、src/libs.rs に書くとのことでした。

Whenever you want to call JavaScript functions, you can add them to this file, and wasm-bindgen sets everything up for you.

環境が変わりましたので p9 までワープ。

rust-wasm templateを見ながらパッケージを追加。index.html, index.js, lib.rs も合わせる。

cargo add --dev wasm-bindgen-test wasm-bindgen-futures futures js-sys
cargo add wee_alloc console_error_panic_hook web-sys

最終的には、以下になりました。(後日また変更することになりますが)

[package]
name = "walk-the-dog"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[profile.release]
# This makes the compiled code faster and smaller, but it makes compiling slower,
# so it's only enabled in release mode.
lto = true

[features]
# If you uncomment this line, it will enable `wee_alloc`:
#default = ["wee_alloc"]

[dependencies]
console_error_panic_hook = "0.1.7"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = [
        "console"
    ]}
wee_alloc = "0.4.5"

[dev-dependencies]
futures = "0.3.31"
js-sys = "0.3.83"
wasm-bindgen-futures = "0.4.56"
wasm-bindgen-test = "0.3.56"
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?