Learning and playing with Rust Programming Language.
https://www.rust-lang.org/learn/get-started
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
If your project isn't in the root you have to tell the language server where to find your files.
mkdir .vscode
# if empty
cat << EOF > .vscode/settings.json
{
"rust-analyzer.linkedProjects": [
"/workspaces/rust_playground/examples/cli/Cargo.toml",
"/workspaces/rust_playground/examples/hello/Cargo.toml",
"/workspaces/rust_playground/examples/tests/Cargo.toml",
"/workspaces/rust_playground/examples/web/Cargo.toml"
]
}
EOF
# if existing settings.json
jq '."rust-analyzer.linkedProjects" +=
[
"/workspaces/rust_playground/examples/cli/Cargo.toml",
"/workspaces/rust_playground/examples/hello/Cargo.toml",
"/workspaces/rust_playground/examples/tests/Cargo.toml",
"/workspaces/rust_playground/examples/web/Cargo.toml"
]' \
config.json > output.json
https://github.com/github/gitignore/blob/main/Rust.gitignore
cargo new hello-rust
cargo run
#cargo add <name>
cargo add clap
#rustc
#cargo rustc
## build for local testing larger binary with debug.
## outputs to target/debug/<name> by default
cargo build
###
## build for sharing much smaller binary
## outputs to target/release/<name> by default
cargo build --release
rustc src/main.rs
These docs will be the basis for the examples below:
https://doc.rust-lang.org/book/
https://www.rust-lang.org/learn
https://github.com/rust-lang/rustlings/
https://doc.rust-lang.org/stable/rust-by-example/
├── examples
│ ├── cli
│ ├── hello
│ ├── tests
│ └── web
https://doc.rust-lang.org/book/ch11-00-testing.html
Trying to start all my code here with the tests and not just the examples.
https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-directory
"unit tests go in the same files as the code"
In the examples this is src/lib.rs, but in their guides its src/main.rs
It looks like this is tied to the intent of the module, so if you do cargo new you get main.rs and if you do cargo new --lib you get lib.rs for a library.
<module name>
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
└── tests
└── integration_test.rs
// config for a test
#[cfg(test)]
// test function blocks
#[test]
// negative ( expect to fail)
#[should_panic]
#test all
cargo test
# test with args
cargo test -- --show-output
# test one assumes test/integration_test.rs exists
cargo test --test integration_test
You can’t use the #[should_panic] annotation on tests that use Result<T, E>. To assert that an operation returns an Err variant, don’t use the question mark operator on the Result<T, E> value. Instead, use assert!(value.is_err()).