今年の言語はRust その３1


# 今年の言語はRust その３1

Rustを学びます

Rustの日本語ドキュメント 2nd Edition
https://doc.rust-jp.rs/book/second-edition/

オリジナル(英語)
https://doc.rust-lang.org/book/


## 実行環境

```sh
$ cargo -V
cargo 1.33.0 (f099fe94b 2019-02-12)

$ rustup -V
rustup 1.17.0 (069c88ed6 2019-03-05)

$ rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28)

$  cat /proc/version
Linux version 4.14.97-74.72.amzn1.x86_64 (mockbuild@gobi-build-64002) 
(gcc version 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC)) 
#1 SMP Tue Feb 5 20:59:30 UTC 2019

$ uname -a
Linux ip-10-100-0-8 4.14.97-74.72.amzn1.x86_64 
#1 SMP Tue Feb 5 20:59:30 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/\*release
NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
Amazon Linux AMI release 2018.03

```


## 12 入出力プロジェクト
コマンドラインプログラムを構築する

### 12.4 テスト駆動開発でライブラリの機能開発

#### 失敗するプログラム


```rust

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    vec![]
}

#[cfg(test)]
mod test {i
    use super::*;
    
    #[test]
    fn one_result() {
        let query = "duct";
        
        let contents = "\
        Rust;
        safe,fast, productive.
        Pick three.";
        
        assert_eq!(
            vec!["safe, fast, productive."],
            search(query, contents)
        );
    }
}
```

#### テストを通過させるコードを書く

```rust
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {

    let mut results = Vec::new();

    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
    
    results
}

```

```bash
$ cargo test
   Compiling minigrep v0.1.0 (/home/ec2-user/environment/minigrep)
warning: unused variable: `err`
  --> src/main.rs:12:53
   |
12 |     let config = Config::new(&args).unwrap_or_else(|err|{
   |                                                     ^^^ help: consider using `_err` instead
   |
   = note: #[warn(unused_variables)] on by default

warning: unused variable: `e`
  --> src/main.rs:19:16
   |
19 |     if let Err(e) = minigrep::run(config) {
   |                ^ help: consider using `_e` instead

    Finished dev [unoptimized + debuginfo] target(s) in 1.06s
     Running target/debug/deps/minigrep-91b7adfcd73a2a20

running 1 test
test test::one_result ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/minigrep-d5caa26a8f36fdf0

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests minigrep

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```


#### run関数内でsearch関数を使用する


```rust


pub fn run(config: Config) -> Result<(), Box<Error>>{

    let mut f = File::open(config.filename)?;
    
    let mut contents = String::new();
    f.read_to_string(&mut contents)?;
    
    for line in search(&config.query, &contents){
        println!("{}", line);
    }
    
    Ok(())
}


```

さて__minigrep__ を使ってみます

```bash
 $ cargo run body poem.txt

I'm nobody! Who are you?
Are you nobody, too?
How dreary to be somebody!

```

```bash
 $ cargo run monomorphization poem.txt
 // 何もなし
```

コマンドラインアプリケーションの基本構成が分かった！

これで、便利アプリが作れるぞ！


イエス！