0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rustコンパイル / プログラム実行

Last updated at Posted at 2023-10-24

Rustのコンパイル/プログラム実行

公式サイトを参考にして、コンパイル/プログラム実行の手順をまとめている。

簡単なRustのコードを作成する

下記HelloWorldのコードを作成する。(main.rs)

// Hello World

fn main(){
    println!("Hello World!")
}

rustcコマンドにてコードをコンパイルする

rustcコマンドを使用して、main.rsファイルをコンパイルする。

rustc main.rs

実行後、mainファイルが存在することを確認する。

drwxrwxr-x 2 vscode vscode    4096 Oct 24 12:31 .devcontainer
drwxr-xr-x 8 vscode vscode    4096 Oct 24 12:39 .git
-rwxr-xr-x 1 vscode vscode 4655232 Oct 24 12:44 main
-rw-r--r-- 1 vscode vscode      57 Oct 24 12:43 main.rs

Rustのプログラムを実行する

下記コマンドを実行することでプログラムを実行する。

./main

下記HelloWorldが表示されることを確認する。

vscode ➜ /workspaces/rust_devcontainer (master) $ ./main

Hello World!

Rustのビルドシステム兼パッケージマネージャー(cargo)を使用してプログラムを実行する

プロジェクトを作成する

下記コマンドを実行して、プロジェクトを作成する。

cargo new hello_cargo

プロジェクトが作成されていることを確認する。(hello_cargo)

vscode ➜ /workspaces/rust_devcontainer (master) $ cargo new hello_cargocargo new hello_cargo
     Created binary (application) `HelloCargo` package
vscode ➜ /workspaces/rust_devcontainer (master) $ 
vscode ➜ /workspaces/rust_devcontainer (master) $ ls -la 
total 4572
drwxrwxr-x 5 vscode vscode    4096 Oct 24 12:46 .
drwxr-xr-x 3 root   root      4096 Oct 24 12:37 ..
drwxrwxr-x 2 vscode vscode    4096 Oct 24 12:31 .devcontainer
drwxr-xr-x 8 vscode vscode    4096 Oct 24 12:39 .git
drwxr-xr-x 3 vscode vscode    4096 Oct 24 12:46 hello_cargo
-rwxr-xr-x 1 vscode vscode 4655232 Oct 24 12:44 main
-rw-r--r-- 1 vscode vscode      57 Oct 24 12:43 main.rs

ディレクトリおよびmain.rsのコードを確認する

下記ディレクトリにて作成されていることを確認する。

vscode ➜ /workspaces/rust_devcontainer (master) $ ls -la hello_cargo/src/
total 12
drwxr-xr-x 2 vscode vscode 4096 Oct 24 12:46 .
drwxr-xr-x 3 vscode vscode 4096 Oct 24 12:46 ..
-rw-r--r-- 1 vscode vscode   45 Oct 24 12:46 main.rs

作成初期の場合、main.rsにHelloWorldのコードが記載されているため確認する。

fn main() {
    println!("Hello, world!");
}

プロジェクトをビルドする

hello_cargoディレクトリ配下にて、下記コマンドを実行してビルドする。

cargo build

下記の通りビルドされる

vscode ➜ /workspaces/rust_devcontainer/hello_cargo (master) $ cargo build
   Compiling hello_cargo v0.1.0 (/workspaces/rust_devcontainer/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
vscode ➜ /workspaces/rust_devcontainer/hello_cargo (master) $

プロジェクトをビルドして、エラーがないか確認する(バイナリデータは生成しない)

hello_cargoディレクトリ配下にて、下記コマンドを実行してコードをチェックする。

cargo check

下記の通りチェックされる

vscode ➜ /workspaces/rust_devcontainer/hello_cargo (master) $ cargo check
    Checking hello_cargo v0.1.0 (/workspaces/rust_devcontainer/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

プログラムをビルドして、実行する

HelloCargoディレクトリ配下にて、下記コマンドを実行してプログラムを実行する。(ビルドと実行)

‘Hello, world!’が表示される。

cargo run

下記の通りビルド後、実行される

vscode ➜ /workspaces/rust_devcontainer/hello_cargo (master) $ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_cargo`
Hello, world!

参考:cargo詳細

  • cargo newを使ってプロジェクトを作成できる
  • cargo buildを使ってプロジェクトをビルドできる
  • cargo runを使うとプロジェクトのビルドと実行を1ステップで行える
  • cargo checkを使うとバイナリを生成せずにプロジェクトをビルドして、エラーがないか確認できる
  • Cargoは、ビルドの成果物をコードと同じディレクトリに保存するのではなく、target/debugディレクトリに格納する

以上がコンパイル / プログラム実行の手順になる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?