LoginSignup
9
6

More than 3 years have passed since last update.

この記事は セゾン情報システムズ Advent Calendar 2020 6日目の記事です。

Rust がいい感じなのでやりましょう!を言いたい。

とりあえず最短で動かす(Windows のみ)

Windows での環境構築手順を説明します。

インストーラをダウンロードしてインストール

https://www.rust-lang.org/ja/learn/get-started
こちらからインストーラをダウンロードして、インストール。

インストール確認

インストールが完了したらコマンドプロンプトを開き、以下コマンドを実行して成功するか確認。
rustc --version
cargo --version

hello, world

  1. 適当なフォルダで hello.rs ファイルを作成する
  2. エディタでファイルを開き以下を打ち込む

    fn main() {
        println!("Hello, world!");
    }
    
  3. コマンドプロンプトを開き、ファイルが存在するフォルダに移動する。

  4. rustc hello.rs を実行

  5. 同じフォルダに hello.exe が生成されるので、実行する。

Cargo も使う

実際に開発するときは Cargo をほぼ使います。Cargo はビルドシステム兼パッケージ管理ツールです。
(node.js の npm や、maven の dependencies 的な存在)
1. コマンドプロンプトで適当なフォルダに移動
2. cargo new hello_cargo を実行
3. hello_cargo ディレクトリが生成されるので Visual Studio Code でフォルダを開く
→ Cargo.toml(package.json や pom.xml 的なファイル) やソースコードが生成される
4. コマンドプロンプトや vscode のターミナルで cargo build を実行
→ target/debug 以下に exe が生成される

5. cargo run で実行できる

改めて Rust とは?

  • プログラミング言語の一種です。C 言語の細かさと JavaScript の手軽さを併せ持っています。
  • コンパイルすることで実行可能なバイナリ(exe)を生成します。なので高速!
  • CLIアプリケーション、バックエンドサーバ、Web アプリケーション、WebAssembly など対応範囲が広い。
  • クレート(https://crates.io/) と呼ばれるパッケージ群が豊富
    • npm, maven 的な存在。
  • ソースコードフォーマッタ、テストも標準装備。
  • 詳しくはドキュメントを!

学ぶには?

サンプル

試しに作った csv を json に変換してみるプログラムです。
Cargo.toml に csv パーサや json を扱うパッケージを追加しています

インプットCSV
id,category,name,type
1,プログラミング,Rust,開発
2,プログラミング,TypeScript,開発
3,プログラミング,C++,開発
4,クラウド,AWS,知識
5,クラウド,Azure,知識
6,クラウド,GCP,知識

アウトプットjson
{
  "categories": [
    "プログラミング",
    "クラウド"
  ],
  "skills": [
    {
      "category": "プログラミング",
      "id": "1",
      "name": "Rust",
      "type": "開発"
    },
    :
    :
    {
      "category": "クラウド",
      "id": "6",
      "name": "GCP",
      "type": "知識"
    }
  ]
}

Cargo.toml
[package]
name = "csv2json"
version = "0.1.0"
authors = ["Yoshihiro-Hirose <--------------------->"]
edition = "2018"

[dependencies]
csv = "1.1.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sample.rs
// #[derive(serde::Serialize, Deserialize, Debug)]
#[derive(Debug, Serialize, Deserialize)]
struct Skill {
    id: String,
    category: String,
    name: String,
    r#type: String,
}

fn example(file: &String) -> Result<(), Box<dyn Error>> {
    let mut rdr = csv::Reader::from_path(file)?;
    let mut categories = HashSet::new();
    let mut skills = vec![];
    for result in rdr.records() {
        let record = result?;

        let category = record.get(1).unwrap();
        categories.insert(category.to_string());

        let skill: Skill = Skill {
            id: record.get(0).unwrap().to_string(),
            category: category.to_string(),
            name: record.get(2).unwrap().to_string(),
            r#type: record.get(3).unwrap().to_string(),
        };
        skills.push(skill)
    }
    let mut master = Map::new();
    master.insert("categories".to_string(), json!(categories));
    master.insert("skills".to_string(), json!(skills));
    let json = serde_json::to_string_pretty(&master)?;
    println!("{}", json);
    Ok(())
}

まとめ

ゆるゆるでしたが以上です。Rust やっていきましょう!
社内の経験者がまだ少なそうなので Rust のラストマンを目指していきます( ・´ー・`)

明日の セゾン情報システムズ Advent Calendar 2020 もお楽しみに!

9
6
2

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
6