---
title: "Cargo入門 | Bitterless Rust"
description: "RustのビルドツールCargoの基本操作 Cargo入門 | Bitterless Rustについて，ubugeeei が wtrclred に書いた記事です．"
last_updated: "2026-11-15"
canonical_url: "https://wtrclred.vercel.app/ja/books/bitterless-rust/02"
language: "ja"
---

# Cargo入門 | Bitterless Rust

Turn on watercolor background effects. * May affect performance, battery life, and temperature on some devices.

### 目次

- [Cargoとは](#cargoとは)
- [プロジェクトを作る](#プロジェクトを作る)
- [Cargo.toml](#cargo-toml)
- [main.rs](#main-rs)
- [ビルドと実行](#ビルドと実行)
- [外部クレートを追加する](#外部クレートを追加する)

## Cargoとは

 CargoはRustのビルドツール兼パッケージマネージャ．
JavaScriptでいうnpm，Pythonでいうpip + venvみたいなもの．
Rustを書くならCargoを使う．それだけ覚えておけばいい．

## プロジェクトを 作る

 

```sh
  cargo   new   hello  -  rust
```

 これで`hello-rust`というディレクトリができる．中身はこう:

 

```text
 hello-rust/
├── Cargo.toml
└── src/
    └── main.rs
```

 たった2ファイル．シンプル．

## Cargo.toml

 

```toml
  [package] 
 name   =   "hello-rust" 
 version   =   "0.1.0" 
 edition   =   "2021" 

 [dependencies]
```

 `[package]`はプロジェクトの情報．`[dependencies]`は外部ライブラリ（クレート）を書く場所．今は空でいい．

> 語弊注意 : edition はRustの「年度版」みたいなもの．深く考えなくていい． 2021 のままでOK．

## main.rs

 

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

 `fn main()` がプログラムのエントリーポイント．`println!` は文字列を出力するやつ．末尾の `!` はマクロという仕組みだけど，今は「`println`には `!` が必要」とだけ覚えておこう．

## ビルドと 実行

 

```sh
  cd   hello  -  rust 
 cargo   run
```

 出力:

 

```text
    Compiling hello-rust v0.1.0 (/path/to/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/hello-rust`
Hello, world!
```

 `cargo run`はビルドと実行を一発でやってくれる．

 他にも覚えておくと便利なコマンド:

 コマンド
 やること

 `cargo run`
 ビルドして実行

 `cargo build`
 ビルドだけ

 `cargo check`
 コンパイルできるかチェック（ビルドはしない．速い）

 普段の開発では`cargo run`と`cargo check`だけ使えば十分．

## 外部クレートを 追加する

 後の章で使うことはないけど，知識として．外部ライブラリを追加するには:

 

```sh
  cargo   add   serde
```

 すると`Cargo.toml`の`[dependencies]`に自動追加される:

 

```toml
  [dependencies] 
 serde   =   "1.0"
```

 [crates.io](https://crates.io/)で使えるクレートを探せる．npmみたいなもの．

 これでCargoの基本は完了．次はRustのコードを書いていこう．

 [← 前の章 はじめに](/ja/books/bitterless-rust/01)

 [次の章 → 変数と型](/ja/books/bitterless-rust/03)

### 関連コンテンツ

- chapter [CDNで Vueを 導入する](/ja/books/progressive-vue/02) #vue #cdn #esm
- chapter [ツールの オールインワン](/ja/books/wonderfull-rust/10) #cargo #clippy #rustfmt

[← 本に戻る](/ja/books/bitterless-rust)

## Sitemap

- [Browse every page on wtrclred](https://wtrclred.vercel.app/sitemap.md)
