Hello, Rust!

Hello, Rust!

So this is my first post about my journey on learning Rust.

Rust is multi-paradigm system language focused on safety. Rust syntax was influenced by C++, but is designed to provide better memory safety while maintaining high performance.

I will not talk about Memory Management here, let me write it in another post.

As the title says, this post is about print "Hello, Rust!" to the console/stdout. I'm assuming you already familiar with programming, so I will skip the installation step & in-depth explanation about the code below.

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

Let's compile it.

$ rustc main.rs -o hello
$ ./hello
Hello, Rust!

Like another "compiled language", we need a function as an "entry point". Usually the function name was main, like what we did on c, c++, etc.

Something interesting from code above was println! , that is not the method. But, that was a macro like #include<stdio.h> in C. Macro basically is a "code generator". Let me explain you in short.

Let's say we have this syntax (in JavaScript):

log('Hello, JS!')

If log is a function like this:

const log = text => console.log(text)

log('Hello, JS!') // calling log method even in "runtime"

The "compiler" give you the exact result. So the log is a function that has runtime. But if log is a "macro", the compiler will transform the code to this (for example):

console.log('Hello, JS!')

console.log is a "standard method" to print something to the console/stdout.

This is how it looks like on Rust while the compiler "generate" the println! macro:

fn main() {
  {
    ::std::io::_print(::core::fmt::Arguments::new_v1(&["Hello, Rust!\n"],
                                                     &match () {
                                                       () => [],
                                                     }));
  };
}

_print was the private method from io.

For now let's leave the "internal" part since I'm new with Rust.

We learned a lot about println! and macro. In next post I will try to explain "borrowing" concept in Rust, which is something new to me.

For notes, I will skip the topics about "primitive types" since that was a fundamental concept in programming, not specific to programming language.

Thanks for reading!

Discussion here.

Repo here.