Trying out Rust
- Install Steps
- Pro-tip, run
rustup update
to update your installation. - Check version do this:
rustc --version
rustc myfunc.rs
in this case, the<yourfile>.rs
is what is compiled.- Next you run it: ./myfunc.rs
- Do you have it?
cargo --version
? cargo new hello_cargo && cd hello_cargo
- make the function in Rust accept the options from the parser.
- Using this lib: https://crates.io/crates/wikipedia
Goal: Build a cli clap tool that generates random numbers in a range
To run it: cargo run -- -x 1 -y 10
To run help:
cargo run -- --help
The output should be:
Generates a Random Number between an X and Y Range
Usage: randocli [OPTIONS]
Options:
-x, --x <X> The lower bound of the range [default: 0]
-y, --y <Y> The upper bound of the range [default: 100]
-h, --help Print help information
-V, --version Print version information
- Added setup script for configuring codespaces: https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html
- Shoutout the rust-cli book
cargo new grrs
To run this tool do the following and you will see this output:
cargo run -- fancy foo.txt
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/grrs fancy foo.txt`
Searching for Pattern: fancy
Found Pattern: fancy
fancy
- Environment seems totally fubar, figure out how to make one that isn't.
- Also figure out how to not checkin debug files
cargo install cargo --force
-
Create is smallest container (contain main)
-
Library crates don’t have a main function, and they don’t compile to an executable.
-
A package is a bundle of one or more crates that provides a set of functionality
-
Cargo.toml file that describes how to build those crates
cargo new backyard
TBD: Still not working...dive into this more next time: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
- The target binary is in target/debug. But, should I run this for day to day or should I use Cargo?
- change the way you pass things in: https://stackoverflow.com/questions/73364389/i-cant-seem-to-get-my-clap-parser-to-take-in-a-vector-of-string-and-use-flags
The way to run a cli via cargo is with these fancy --
and then don't say name of tool
cargo run -- --name "bob"
Fixed random choices and started ownership
We fixed it!
Fix this CODE!!!
// Looping fun
// Thank you: https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
use rand::Rng;
// Broken code fix: learn to convert to iterator
fn main() {
//let a = [10, 20, 30, 40, 50];
let mut rng = rand::thread_rng();
for element in rng {
println!("the value is: {element}");
}
}
Make a function:
cargo new hello_func && cd hello_func
(put some code in src/main like this)
//Function 1
fn main() {
println!("Hello, world!");
another_function();
}
//Function 2
fn another_function() {
println!("Another function.");
}
cargo build
Quick example:
use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello fellow Rustaceans!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(message.as_bytes(), width, &mut writer).unwrap();
}