備忘録

調べ物の備忘録など何か書き残し。

Rustのhelloworldほか

rustのhelloworldを作って動かしてみる。

プロジェクト作成

1.空のプロジェクトを作成

>cargo new helloworld
     Created binary (application) `helloworld` package

>cd helloworld

2.自動生成のコードをチェック

>type src\main.rs
fn main() {
    println!("Hello, world!");
}

通常実行

1.コンパイル&実行

>cargo run
   Compiling helloworld v0.1.0 (C:\...\helloworld)
    Finished dev [unoptimized + debuginfo] target(s) in 0.70s
     Running `target\debug\helloworld.exe helloworld`
Hello, world!

cargoの機能を試してみる

ひとまずcargoの機能を見てみる。 通常のrunのほか、いくつか立ち上げ方があるようなので試してみる。

>cargo help
Rust's package manager

USAGE:
    cargo [+toolchain] [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -V, --version                  Print version info and exit
        --list                     List installed commands
        --explain <CODE>           Run `rustc --explain CODE`
    -v, --verbose                  Use verbose output (-vv very verbose/build.rs output)
    -q, --quiet                    No output printed to stdout
        --color <WHEN>             Coloring: auto, always, never
        --frozen                   Require Cargo.lock and cache are up to date
        --locked                   Require Cargo.lock is up to date
        --offline                  Run without accessing the network
        --config <KEY=VALUE>...    Override a configuration value (unstable)
    -Z <FLAG>...                   Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help                     Prints help information

Some common cargo commands are (see all commands with --list):
    build, b    Compile the current package
    check, c    Analyze the current package and report errors, but don't build object files
    clean       Remove the target directory
    doc         Build this package's and its dependencies' documentation
    new         Create a new cargo package
    init        Create a new cargo package in an existing directory
    run, r      Run a binary or example of the local package
    test, t     Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock
    search      Search registry for crates
    publish     Package and upload this package to the registry
    install     Install a Rust binary. Default location is $HOME/.cargo/bin
    uninstall   Uninstall a Rust binary

See 'cargo help <command>' for more information on a specific command.

機能がいくつもありそうだが、ひとまず手ごろそうなdoc, testを試してみる。 多分 docがドキュメント作成、testがテストコードの実行 ?

cargo doc

1."cargo doc"を実行してみる。

> cargo doc
>dir target\doc\helloworld
 all.html
 fn.main.html
 index.html
 sidebar-items.js

2.出てきたindex.htmlを開いてみる。

>target\doc\helloworld\index.html

 →*.htmlに紐づけたブラウザでindex.htmlが開けた。

f:id:matomoto:20210718143031p:plain

3.コードをいじってみる。

+/// これはmainです
fn main() {
    println!("Hello, world!");
}
> cargo doc
> target\doc\helloworld\index.html

f:id:matomoto:20210718143420p:plain
→コメントを追加できた。

cargo test

1.cargo testを実行してみる

>cargo test
   Compiling helloworld v0.1.0 (C:\...\helloworld)
    Finished test [unoptimized + debuginfo] target(s) in 0.52s
     Running unittests (target\debug\deps\helloworld-2f37b4d133137ab4.exe)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

2.コードをいじってみる(正常)

+#[cfg(test)]
+mod tests {
+    #[test]
+    fn my_test() {
+        assert_eq!(2 + 2, 4);
+    }
+}
+
fn main() {
    println!("Hello, world!");
}
> cargo test
   Compiling helloworld v0.1.0 (C:\...\helloworld)
    Finished test [unoptimized + debuginfo] target(s) in 0.52s
     Running unittests (target\debug\deps\helloworld-2f37b4d133137ab4.exe)

running 1 test
test tests::my_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

3.さらにコードをいじってみる(異常コード追加)

#[cfg(test)]
mod tests {
    #[test]
    fn my_test() {
        assert_eq!(2 + 2, 4);
    }
+    #[test]
+    fn my_test_case2() {
+        assert_eq!(2 + 2, 3);
+    }
}

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

fn main() {
    println!("Hello, world!");
}
>cargo test
   Compiling helloworld v0.1.0 (C:\...\helloworld)
    Finished test [unoptimized + debuginfo] target(s) in 0.17s
     Running unittests (target\debug\deps\helloworld-2f37b4d133137ab4.exe)

running 2 tests
test tests::my_test ... ok
test tests::my_test_case2 ... FAILED

failures:

---- tests::my_test_case2 stdout ----
thread 'tests::my_test_case2' panicked at 'assertion failed: `(left == right)`
  left: `4`,
 right: `3`', src\main.rs:9:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::my_test_case2

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--bin helloworld'

→エラーが検出された .. かな?