What the RUST is this!? š¦
Published Sep 19 2023
Listen, I'll be honest here. I'm not even sure why I am trying to learn this language. But I found myself on the website and I just thought about giving it a go. I am super persimestic at the moment and I just want this to go quickly so I can tick a box and go back to the stuff I am comfortable with.
Ok, let's figure out what's what. I am going to try install it first and then maybe learn one thing for today.
I'm using a MAC so according to the website I need to run this:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Done.
Ok once thats done, the documentation says I run the following command to create a project:
cargo new hello-rust
Done.
Ok so this is the directory structure:
hello-rust
|- Cargo.toml
|- src
|- main.rs
Ok so apparently Cargo.toml
is where I keep metadata and dependencies. So I'm guessing that this would be the package.json of RUST? I mean they could have just called it package.toml
to keep things simple š, but what do I know, I'm just a PHP guy.
Ok main.rs
is pretty much self explanatory. It's where we write some code.
Lets learn some stuff.
Dumb Questions ā
Why does RUST wrap it's code with fn main() {}
?
This is what ChatGPT says:
fn main() {}
is the designated entry point for a Rust program. When you run a Rust executable, the operating system starts executing from this function.
Does the entry file name have to be main.rs
?
Ok ChatGPT says no. It can be named differently, but there are specific rules and conventions you need to follow blah blah blah. Ok we'll learn about these at a later point.
Setting a goal
Ok let's take some baby steps and figured out what we wanna take away today. I want to learn about:
- Cargo Basics
- Print Output
Yup, that's it. Let's do this...
Cargo Basics
Ok I think for this section, we'll just create a table to remind us of the important commands
| Command | Command |
| ------------ | ---------------------------------------------------------------------- |
| cargo new | Create a new Rust project with basic directory structure |
| cargo build | Build (compile) the current project and generate a binary executable |
| cargo run | Build and run your current project (cargo build + run) |
| cargo check | Build the current project without generating a binary executable |
| cargo add | Add a new dependency and include it in Cargo.toml file |
| cargo update | Update all dependencies of current project to latest version |
Print Output
Ok let's figure out how to print something.
BTW: Print! is a macro, not a function š
So yeah, there's a difference between macros
and functions
. But this extends beyound the scope of this post, so I'll learn it later š...I hate RUST already.
Simple Use:
fn main() {
print!("Hello,");
print!(" world!");
}
// Hello, world!
If you want to add a new line after each macro, you can use println
;
fn main() {
println!("Hello...");
println!("World!");
}
/**
* Hello...
* World!
*/
But you can also use \n
like this:
fn main() {
print!("Hello...\nWorld!");
}
/**
* Hello...
* World!
*/
Nifty!
Printing Variables
Ok, this is just silly, so this is how we print variables:
fn main() {
let age = 31;
println!("{}", age);
print!("{}", age);
}
So basically, think of {}
as the variable slot.
My question is what happens when you have multiple slots?
fn main() {
let age = 31;
print!("{} {}", age);
}
šššššš
error: 2 positional arguments in format string, but there is 1 argument
--> src/main.rs:7:13
|
7 | print!("{} {}", age);
| ^^ ^^ ---
error: could not compile `hello-rust` (bin "hello-rust") due to previous error
Ok so these "slots" are actually called "positional arguments"...mmkay...
Let's see how else we can use these positional arguments...
fn main() {
let age = 31;
let name = "Elvis";
print!("{} {}", age, name);
}
// 31 Elvis
We can also switch these around by using argument position...
fn main() {
let age = 31;
let name = "Elvis";
print!("{1} {0}", age, name);
}
// Elvis 31
We can also use named positional arguments
fn main() {
let age = 31;
let name = "Elvis";
print!("{age} {name}");
}
// 31 Elvis
Ok, that's it for now. Until next time!