Thursday, April 14, 2022

Go vs Rust - Hello World

 1 Go

/*
 * Add the package main in your program to tells the compiler that
 * the package must compile in the executable program rather than a shared library.
 * It is the starting point of the program and also contains the main function in it.
 */
package main

/*
 * After adding main package import “fmt” package that is used to implement
 * formatted Input/Output with functions.
 */
import "fmt"

func main() {
    fmt.Println("Hello, World!")
}


2 Rust
fn main() {
/*
 * println! calls a Rust macro. If it called a function instead,
 * it would be entered as println (without the !).
 */
    println!("Hello, world!");
}


  • Go need define self package, import "fmt" package and then call its Println method. But rust directly call macro println! from std::from module.
  • Go use keyword func to define the function. But rust use fn.
  • Go don't need ';' when only one sentence in a line. But rust need it (there are exceptions).
  • For these code snippet, rust seems be more concise anyway.

No comments:

Post a Comment