Rust
is a multi-paradigm, general-purpose programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++
, but guarantees memory safety by using a borrow checker to validate references. Overall, it is a language worth learning with a supportive community and hereon in I’ll be documenting my Rust learning journey !
Installing & Understanding the Ecosystem.
🧑💻 Go to rust-lang.org, and simply follow the instructions.
- Install and manage Rust using the
rustup
tool. If you already have Rust installed & want to check the version, use :
|
|
rustc
is the Rust compiler. To update to the current stable version ofrustc
compiler, use :
|
|
- If you’ve installed Rust using
rustup
, it’ll come withcargo
, the Rust package manager.
Using cargo
is stupidly simple,
- To create a new package (project) with
cargo
, use
|
|
This creates a rust project with a binary program as output.
|
|
This program can be ran using cargo
run command while in the project directory.
|
|
While creating Library package, use cargo
new with --lib
|
|
follow the same procedure as the binary program to compile the library
|
|
That’s about it !! You can use your favorite text editor (Vim, VS Code, etc.) with rustfmt
(Rust code formatter) or RLS
(Rust Language Server) or my favorite Rust Analyzer.
Rust is an opinionated language, it always helps to have these tools while developing.
Learning Resources
Rust language has excellent and completely free learning resources such as 🔽
[1] The (official) Rust Language Book
[2] Rustlings
- 🦀 Small exercises to get used to reading and writing Rust code!
[3] Rust by Example
- Illustrates various Rust concepts & std-lib with examples!
I’ve found Jason Orendorff and Jim Blandy’s book Programming Rust: Fast, Safe Systems Development
very useful and would highly recommend it to anyone who seriously wants to learn Rust.
Greeting world (as per tradition) !
“Hello World!” by Brian Kernighan
To print “Hello World!”, a string of type &str
in Rust we need to use what Rust calls macros. Macro used in Rust to print with newline (\n
) character is println!
(note Rust macros end with !
(bang)).
Let’s create package/crate named ‘hello’ for this simple project. Using cargo
,
|
|
This will create package with name ‘hello’. Now edit the src/main.rs
file to 🔽
|
|
To run this, use cargo run
command. The output should resemble this 🔽