今年の言語はRust その２９


# 今年の言語はRust その２９

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.1 コマンドライン引数を受け取る

まずは定番の __args__ からコマンドライン引数を受け取ります。

```rust
use std::env;

fn main() {

    let args: Vec<String> = env::args().collect();
    
    println!("{:?}", args);
    
}


```

```rust
 $ cargo run searchstring example-filename.txt
   Compiling minigrep v0.1.0 (/home/ec2-user/environment/minigrep)
    Finished dev [unoptimized + debuginfo] target(s) in 0.51s
     Running `target/debug/minigrep searchstring example-filename.txt`
["target/debug/minigrep", "searchstring", "example-filename.txt"]
```
これはテストできないのかな


### 引数を保存


```rust
use std::env;

fn main() {

    let args: Vec<String> = env::args().collect();
    
    let query = &args[1];
    let filename = &args[2];
    
    println!("Searching for {}", query);
    
    println!("In file {}", filename);
    
    
}

```

```bash
$ cargo run searchstring example-filename.txt
   Compiling minigrep v0.1.0 (/home/ec2-user/environment/minigrep)
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
     Running `target/debug/minigrep searchstring example-filename.txt`
Searching for searchstring
In file example-filename.txt
```

### 12.2 ファイルを読み込む


```rust

use std::env;
use std::fs::File;
use std::io::prelude::*;


fn main() {

    let args: Vec<String> = env::args().collect();
    
    let query = &args[1];
    let filename = &args[2];
    
    println!("Searching for {}", query);
    
    println!("In file {}", filename);
    
    let mut f = File::open(filename).expect("file not found");
    
    let mut contents = String::new();
    f.read_to_string(&mut contents)
        .expect("something went wrong reading the file");
    
    println!("With text:\n{}", contents);
    
}

```

```rust
 $ cargo run searchstring poem.txt
   Compiling minigrep v0.1.0 (/home/ec2-user/environment/minigrep)
    Finished dev [unoptimized + debuginfo] target(s) in 0.45s
     Running `target/debug/minigrep searchstring poem.txt`
     
Searching for searchstring
In file poem.txt
With text:
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.

How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
```




Rust楽しいすね。


イエス！