英語で読む「Rust, WebAssembly」

年末年始で学んでみたいプログラミング言語特集。今回は個人的に最も思い入れがあって環境を整えチュートリアルを進めているRustとWebAssembly。

まずRustの方から目を通してみる。

Rust is blazingly fast and memory-efficient: with no runtime or garbage collector, it can power performance-critical services, run on embedded devices, and easily integrate with other languages.

まず他の新しい言語と同様、高速であることをアピール。Wikipediaを読むと2006年に登場し、2009年からMozillaのプロジェクトになっている。そして2011年からLLVMがコンパイル基盤になった。

Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

そしてメモリの安全性とスレッドの安全性。

Rust has great documentation, a friendly compiler with useful error messages, and top-notch tooling — an integrated package manager and build tool, smart multi-editor support with auto-completion and type inspections, an auto-formatter, and more.

最後にドキュメント、使いやすいコンパイラ、top-notch(最も優秀)なツール。

最新版では削除されてしまったようだが、古い版のドキュメントでは、「食事する哲学者」という並列処理問題がチュートリアルに含まれていた。

A philosopher picks up the fork on their left.
They then pick up the fork on their right.
They eat.
They return the forks.

「食事する哲学者」は、ざっくりいうと5人の哲学者に5本のフォークが与えられているが、皿に盛られたスパゲッティを食べようとすると2本のフォークが必要で、一斉に食べようとするとデッドロックが発生するという問題。5人のうち1人が左利きだとデッドロックが解消するのがユニークなサンプルだが、簡単な言語だけ選んできた身としては、ちょっと難しいかも。

Specifically, the C abstract machine model is the snuggest fit for most hardware platforms, and it is therefore a good level of abstraction to build low-level system software like interrupt service routines, garbage collectors and virtual memory managers. However, the “user interface” of the C language, in particular its preprocessor and its type system, have aged tremendously. For anyone who learned anything newer, they frankly suck.

POSTDでも紹介されているRustに関するRaphael ‘kena’ PossによるコラムではC言語はgood level of abstraction、つまり割り込みとかガベッジコレクションなど表現するにはいい感じだが、新しい言語を学んだ人にとっては時代遅れだと言っている。

さて、Rustが注目されるきっかけになったのが、WebAssemblyの対応だろう。

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

Wasmはstack-based VMで動くbinary instruction formatで、C/C++/Rustでコンパイルでき、Webクライアントやサーバで動く。ブラウザで動くのは聞いていたがサーバも視野にあるようだ。

現時点でJavaScriptの力を借りないとDOM操作ができない点が残念だが、画面表示はJavaScript、裏のロジックはWebAssemblyという機能配分であれば今でも効果がある。

下記のスライドの通り試してみたが、当然ながら一発で動いてくれた。Rust+WebAssemblyならではの場面を見つけ出してみたい。



この記事が気に入ったらサポートをしてみませんか?