YU2TA7KA's BLOG ~take one step at a time~

派生開発、組み込み開発周りのこと。

Raspberry PiにRustをインストールしてHelloWorldする

夏休みに始める新しいこととして、いろいろあってRustが選ばれました。参考書は実践Rust入門[言語仕様から開発手法まで]です。

参考書の1章を読んだのですが、Rustの特徴を詳細に解説してくれており、これからがワクワクしてきました。C,C++と同レベルの速度パフォーマンスをもち、Rustのコンパイラが安全なプログラミングをサポートしてくれる、というところに魅力を感じます。これからそれらを体感していきたいです。職場の濃厚なソースをいつかRustでスッキリソースに置換していけたら最高だなと思います。

Rustとは

Mozillaが支援する[2][3]オープンソースのシステムプログラミング言語である。
Rust (プログラミング言語) - Wikipedia

Hello World

環境

pi@raspberrypi:~/Rust $ uname -a
Linux raspberrypi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux
pi@raspberrypi:~/Rust $ cat /proc/version
Linux version 4.19.57-v7+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1244 SMP Thu Jul 4 18:45:25 BST 2019
pi@raspberrypi:~/Rust $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.9 (stretch)
Release:        9.9
Codename:       stretch

インストール

pi@raspberrypi:~/Rust $ curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for th
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to
directory, located at:

  /home/pi/.cargo/bin

This path will then be added to your PATH environment varia
profile file located at:

  /home/pi/.profile

You can uninstall at any time with rustup self uninstall an
be reverted.

Current installation options:

   default host triple: armv7-unknown-linux-gnueabihf
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: syncing channel updates for 'stable-armv7-unknown-lin
info: latest update on 2019-07-04, rust version 1.36.0 (a53
info: downloading component 'rustc'
 75.2 MiB /  75.2 MiB (100 %)   5.2 MiB/s in 30s ETA:  0s
info: downloading component 'rust-std'
 65.0 MiB /  65.0 MiB (100 %)   1.1 MiB/s in 33s ETA:  0s
info: downloading component 'cargo'

info: installing component 'rustc'
 75.2 MiB /  75.2 MiB (100 %)   2.6 MiB/s in 39s ETA:  0s
info: installing component 'rust-std'
 65.0 MiB /  65.0 MiB (100 %)   3.2 MiB/s in  1m 33s ETA:
info: installing component 'cargo'
  4.2 MiB /   4.2 MiB (100 %)   3.1 MiB/s in  8s ETA:  0s
info: default toolchain set to 'stable'

  stable installed - rustc 1.36.0 (a53f9df32 2019-07-03)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo
environment variable. Next time you log in this will be don

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

バージョン確認

pi@raspberrypi:~/Rust $  cargo --version
cargo 1.36.0 (c4fcfb725 2019-05-15)
pi@raspberrypi:~/Rust $  rustc --version
rustc 1.36.0 (a53f9df32 2019-07-03)
pi@raspberrypi:~/Rust $ rustdoc --version
rustdoc 1.36.0 (a53f9df32 2019-07-03)
pi@raspberrypi:~/Rust $ rustup --version
rustup 1.18.3 (435397f48 2019-05-22)

パスの設定

pi@raspberrypi:~/Rust $ source $HOME/.cargo/env

ビルド

cargoはRustのビルドマネージャー兼パッケージマネージャです。

pi@raspberrypi:~/Rust $ cargo new hello
     Created binary (application) `hello` package
pi@raspberrypi:~/Rust $ cd hello/
pi@raspberrypi:~/Rust/hello $ cargo build
   Compiling hello v0.1.0 (/home/pi/Rust/hello)
    Finished dev [unoptimized + debuginfo] target(s) in 9.29s

実行

pi@raspberrypi:~/Rust/hello $ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s
     Running `target/debug/hello`
Hello, world!

参考

UbuntuにRustをインストールする - Qiita
作業内容はこちらのコピーになっています。
Home | Rustの日本語ドキュメント/Japanese Docs for Rust
こちらにいろいろドキュメントはまとまっています。
Hello, World! - The Rust Programming Language
HelloWorldの詳細はこちらが参考になります。