• Bilal Khan
  • Posts
  • Rust Essentials: Getting Started with Compilation

Rust Essentials: Getting Started with Compilation

Before going to the Rust syntax, it's important to understand some of the key programming concepts. There are two fundamental types of programming languages you should be aware of:

  • Compiled Languages

  • Dynamic Languages

Compiled vs. Dynamic Languages

Compiled languages convert the code into machine code that the computer can understand, while dynamic languages do not compile the code traditionally but check the types at runtime.

Rust is a compiled language, which means that the code needs to be compiled before it can be executed. This process involves two steps:

  1. Compilation

  2. Execution

Your First Rust Program

Before we run any commands, let's write a simple "Hello, World!" program.

File: main.rs

fn main() {
    println!("Hello, World!");
}

Compiling the Code

Once the program is written, the next step is to compile it. Use the following command:

$ rustc main.rs

After running the command, you'll notice a new file in the directory:

$ ls
main  main.rs
  • main.rs contains your Rust code.

  • main (or main.exe on Windows) contains the compiled machine code.

Note:

  • On Windows, the compiled file will have a .exe extension.

  • On Linux/Mac, it will not have an extension but is still executable.

Running the Executable

To run the compiled program, enter:

$ ./main   # Linux/Mac
$ .\main.exe   # Windows

Expected output:

Hello, World!

The best part?

You can now share this compiled file with others, and they can run it without installing Rust!

While rustc works great for simple programs, as your project grows, you'll need a better way to manage code and dependencies. That's where Cargo, Rust's powerful build tool, comes in.

What's Next?

Stay tuned for the next newsletter, where I'll introduce you to the Cargo tool and how it makes Rust development easier!

If you need any Rust guidance and are just curious about learning Rust then you can check out the YouTube playlist “Rust for Beginners“ where I have covered all of the basic concepts in a simple way for you to understand.

If you're interested to learn Rust then I have a 2-month Rust for Beginners Course that will help you build a solid foundation & build amazing projects. Book a FREE call.

Happy Coding!