Rusty Symbol Soup
I have become interested in Rust after watching a few videos about it.
Rust for Linux is adding Rust in the Linux kernel, Git will also be using Rust, and I’m trying to learn a bit and use it.
Rust Version Management
I’m a fan of asdf but asdf doesn’t do a good job compared to rustup: asdf places cargo-installed binaries in the rust installation directory instead of ~/.cargo/bin, making them more difficult to access from the shell. So that concludes it. I’m recommending rustup.
Rustup can manage Rust versions for you globally. After you install rustup, run this command to switch to stable:
1rustup default stable # or simply `rustup-init` if `rustup` is not available
Trying Rust
Rust uses a borrow checker and is not a garbage-collected language. This sounds simple, but it’s painful to write real world Rust. Here are a few examples.
Derive
Here,
1#[derive(serde::Serialize, Debug)]
2struct Response {
3 message: String,
4}
Why do I need to write
1#[drive(feature)]
instead of
#derive(feature)
?
It’s making derive look like an array.
await.unwrap()
Consider the following:
1let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
2
3println!("Listening at {}", addr);
4
5axum::serve(listener, router).await.unwrap();
Is await a property? Why dot await? And why do I need to unwrap it? Can’t this be one function call?
rand::rng
This is how you generate a random number:
1let mut rng = rand::rng();
2let random_number = rng.random_range(0..array.len())
Do I really need to create a rng every time I use it? Why isn’t there a global one?
&str
1const MOTTOS: [&str; 2] = [
2 "I love, therefore I am.",
3 "Amo ergo sum.",
4];
It’s takes 5 symbols(:[&;]) to declare a type of a string array, compared to 3 in C (*[]):
1char *MOTTOS[2] = {
2 "I love, therefore I am.",
3 "Amo ergo sum."
4};