LoginSignup
9
3

More than 5 years have passed since last update.

はじめに

なんとなくはじめたアドベントカレンダーに付き合ってくれた皆、ありがとう。アドベントカレンダーて思ったより大変。
今日Rustを少し勉強してみたので、アドベントカレンダー27日目の記事ではRusthello 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

VSCode

vscodeでRLS用の拡張を入れる。二種類あるが、次の拡張を推奨。

vscodeでRLS拡張を使う時に注意する点でsettings.jsonについて。

  • RLSで使うrustのchannelを使用しているchannelに設定すること。例えば、betaを使っているならば以下のように設定すること。
settings.js
{
  "rust-client.channel": "beta"
}

プロジェクトを作成

nodejsでいうnpm init的な感じのフロー。Cargo.toml, .gitignoreなど基本セットを生成してくれる。

$ cargo new --bin hello-rust
$ cd hello-rust
$ code .

src以下にmain.rsが作成されているので、このファイルを次のように編集。

src/main.rs
fn main() {
    println!("Hello, Rust!");
}

Hello, Rust!

$ cargo run
output.
Compiling hello-rust v0.1.0
 Finished dev [unoptimized + debuginfo] target(s) in 0.76 secs
   Running `target/debug/hello-rust`
Hello, Rust!

Tutorial

9
3
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
9
3