はじめに
なんとなくはじめたアドベントカレンダーに付き合ってくれた皆、ありがとう。アドベントカレンダーて思ったより大変。
今日Rustを少し勉強してみたので、アドベントカレンダー27日目の記事ではRust
でhello world
をしたいと思う。
環境構築
Rustupのインストール
Rustupはrustのインストールをするためのもの。nodejsでいうnodebrew的な立ち位置。
$ curl https://sh.rustup.rs -sSf | sh
$ source $HOME/.cargo/env
Rustのインストール
Rustにはnightly
, beta
, stable
がある。今回はRLSを使いたいので、本記事を書いた時にrls-preview
があるbeta
版をインストール。nightly
にはrls-preview
が無い時と有る時があるため、そのときはbeta
を使ってみたりするのがよい。
$ rustup install beta
$ rustup default beta
RLS
Rust Language Serverとは、開発手助けツールみたいなもので、補完とか構文解析をしてくれる。
$ rustup component add --toolchain=beta rls-preview
$ rustup component add --toolchain=beta rust-src
$ rustup component add --toolchain=beta rust-analysis
- 参考: https://blog.rust-lang.org/2014/10/30/Stability.html
- 参考: https://qiita.com/skitoy4321/items/0bf6826f948720bed821
VSCode
vscodeでRLS用の拡張を入れる。二種類あるが、次の拡張を推奨。
vscodeでRLS拡張を使う時に注意する点でsettings.json
について。
- RLSで使うrustのchannelを使用しているchannelに設定すること。例えば、
beta
を使っているならば以下のように設定すること。
{
"rust-client.channel": "beta"
}
プロジェクトを作成
nodejsでいうnpm init
的な感じのフロー。Cargo.toml
, .gitignore
など基本セットを生成してくれる。
$ cargo new --bin hello-rust
$ cd hello-rust
$ code .
src
以下にmain.rs
が作成されているので、このファイルを次のように編集。
fn main() {
println!("Hello, Rust!");
}
Hello, Rust!
$ cargo run
Compiling hello-rust v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.76 secs
Running `target/debug/hello-rust`
Hello, Rust!