Rust (Ubuntu 24.04)

はじめに_

WSL2上にUbuntuがインストールされているとする。

参考_

以下の記事に基づきインストールする。

Rustのインストール_

コンパイルに必要なソフトウェアをインストールする→build-essentialとは

% sudo apt install -y build-essential

Rust公式:はじめにに従いインストールする。

% curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

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

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/gotoh/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/gotoh/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/gotoh/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/gotoh/.profile
  /home/gotoh/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
> (後藤注:1を選んだので単にEnterキーを押した)

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2025-05-15, rust version 1.87.0 (17067e9ac 2025-05-09)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 19.9 MiB /  19.9 MiB (100 %)  11.0 MiB/s in  2s
info: downloading component 'rust-std'
 29.4 MiB /  29.4 MiB (100 %)  11.0 MiB/s in  3s
info: downloading component 'rustc'
 76.3 MiB /  76.3 MiB (100 %)  11.0 MiB/s in  7s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.9 MiB /  19.9 MiB (100 %)   8.0 MiB/s in  1s
info: installing component 'rust-std'
 29.4 MiB /  29.4 MiB (100 %)  16.5 MiB/s in  2s
info: installing component 'rustc'
 76.3 MiB /  76.3 MiB (100 %)  17.4 MiB/s in  4s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.87.0 (17067e9ac 2025-05-09)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
source $"($nu.home-path)/.cargo/env.nu"  # For nushell

動作確認する。

% source ~/.bashrc
% which cargo
/home/gotoh/.cargo/bin/cargo

% mkdir -p ~/Sandbox/Rust
% cd ~/Sandbox/Rust
% cargo new --bin prj_evid
     Created binary (application) `prj_evid` package
% cd prj_evid/

% ls -1F
Cargo.toml
src/

ビルドする。

% cargo build
   Compiling prj_evid v0.1.0 (/home/gotoh/Sandbox/Rust/prj_evid)
    Finished dev [unoptimized + debuginfo] target(s) in 1.06s

実行する。

% cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/prj_evid`
Hello, world!

テストを実行する。

% cargo test
   Compiling prj_evid v0.1.0 (/home/gotoh/Sandbox/Rust/prj_evid)
    Finished test [unoptimized + debuginfo] target(s) in 0.29s
     Running unittests src/main.rs (target/debug/deps/prj_evid-fc89aa9c073b01cb)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

VSCodeの設定_

VSCodeのインストール_

以下に従いインストールする。

拡張機能の設定_

VSCodeが起動中の場合は一度終了し、Ubuntu側から起動する。

% code .

原 旅人: コンセプトから理解するRustでおすすめされている、拡張機能をインストールする。

  • rust-analyzer
  • CodeLLDB
  • Statusbar Error

また、rust-analayzerの設定でcargo check を cargo clippyに変更する。

戻る_