Learning Rust Pt. 1

Jan 23, 2023

Rust is the system programming language that run blazingly fast, prevent segfaults, and guarantees thread safety. In other words, Rust is designed for the speed, safety, and concurrency. But, nowadays developers are crazy and use language other than it was originally intended for.

Rustup is the recommended way to install the Rust on the computer. The installation steps are quite easy as running a command in the terminal. Because of that, I do not plan to re-write steps again here. Go ahead and install the Rust on your computer.

Once the installation is done, we should have rustc, cargo, and rustup. rustc is the Rust compiler, cargo is package manager and build system, and rustup is a version manager.

rustc --version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
cargo --version
cargo 1.80.1 (376290515 2024-07-16)
rustup --version
rustup 1.27.1 (54dd3d00f 2024-04-24)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.80.1 (3f5fd8dd4 2024-08-06)`

---

Rust files are normally identified via .rs extension. A journey of the programming language should always start with hello, world program and here is Rust’s one in the hello.rs file.

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

Being compiled programming language, we need to invoke the Rust compiler or rustc on this program to generate an executable.

rustc hello.rs

Compiler creates an executable (if error not found) usually named after the program file without extension that is easy to run!

./hello
hello, world

And there you have the hello, world program in Rust!

---

  1. fn is used to define a function in Rust.
  2. main() function is an important one from where the execution starts from.
  3. println!() is a macro that will print the hello, world text on the terminal with a newline at the end.
  4. If the function name ends with ! e.g. println!(), then it is a macro not a function.
  5. For now, don’t worry about the difference between macro and function (if you are still insisting, here is one answer from StackOverflow that might help you).
  6. Semicolons are required to separate the statements.
Tags: rust