DEV Community

Cover image for Programming Languages war Golang vs Rust Define Variables
Carlos Rivas
Carlos Rivas

Posted on

Programming Languages war Golang vs Rust Define Variables

Define Variables

Hey, my friends, a new post about this war.

I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

In this opportunity I'm going to check up the deep basic information that these languages could hide, I think I'm being dramatic. 😅

We're going to figure out how to define variables.

I'm going to do a little script with all examples and a little more. =D

Variables

It's a space of memory where you could save information what you need to manipulate. These two languages need to define the type of variable in front of the name or not. It's important to say that exist boolean, numbers, strings, and others.

Golang/Go

Before defining variables, I'm going to write about the structure

Do you remember our hello.go

hello.go

package main import "fmt"
func main() {
  fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode
  • The first line: Tells the Go compiler that the package should compile as an executable.
  • Second line: Import fmt, fmt is a core library package that contains functionalities related to formatting and printing output.
  • And the end func main() is the entry point of an executable program.

Now you know theory things now I'm going to make magic. Ok no. I'm going to make code.

magic.gif

It's indispensable that you know that exist var and const like special words to define variables and constants.

Example

const NAME string = "Luffy"
func main() {
  var country string = "Brazil"
  fmt.Printf("Hello, %s from %s\n", NAME, country)
}
Enter fullscreen mode Exit fullscreen mode

But Go is so smart, then doesn't need that you tell him the type, he can discover it.

Example

func main() {
  var color = "Blue"
  var number = 20
  fmt.Printf("Type: %T Value: %v\n", number, number)
  fmt.Printf("Type: %T Value: %v\n", color, color)
}
Enter fullscreen mode Exit fullscreen mode

These are all basic types of variables that exist in Golang

Boolean: bool

String: string

Integers:
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

Byte:
byte // alias for uint8

Rune:
rune // alias for int32
// represents a Unicode code point

Float:
float32 float64

Complex:
complex64 complex128

The last trick that I'm going to use is to disappear the word "var"

func main() {
  crew := "Straw Hat Pirates"
  fmt.Printf("Type: %T Value: %v\n", crew, crew)
}
Enter fullscreen mode Exit fullscreen mode

I almost forget something important, the zero value, what means that?

func main() {
  var i int
  var f float64
  var b bool
  var s string
  fmt.Printf("%v %v %v %q\n", i, f, b, s)
}
Enter fullscreen mode Exit fullscreen mode

Each type of variable has a value by default, Then if you declare a variable, but you don't initialize it, the variable is going to take the default value.

You can use any method to declare a variable, but I recommend that you should specify the datatype to new variables.

challenge-accepted-let-the-challenge-commence.gif

The last challenge if you want to try it. What if?

func main() {
  number := 1 number = "one"
  fmt.Printf("%v \n", number)
}
Enter fullscreen mode Exit fullscreen mode

Rust

Before defining variables, I'm going to write about the structure. What is that, a déjà vu?

Do you remember our main.rs

main.rs

fn main() {
  println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode
  • fn main() is always the first code that runs in every executable Rust program.

Rust has Variables, Variables Mutability, and Constants

It's indispensable that you know that exist let, let mut, and const like special words to define variables and constants.

Example

const NAME: &str = "Nami";
fn main() {
  let country: &str = "Swedish";
  println!("Hello, {} from {}!", NAME, country);
}
Enter fullscreen mode Exit fullscreen mode

The difference between let and let mut, let: you can't change the value of the variable, and let mut you can.

But if "let" you can't change and constant either, what is the difference?

In addition to the declaration are "let" or "const", in the constant the type of the value must be annotated and can be declared in any scope.

Rust is so smart too because it doesn't need that you tell him the type, he can discover it.

Example

use typename::TypeName;
fn main() {
  let color = "Blue";
  let number = 18;
  println!("Type: {}, value: {}", color.type_name_of(), color);
  println!("Type: {}, value: {}", number.type_name_of(), number);
}
Enter fullscreen mode Exit fullscreen mode

Add this like dependence, in the file Cargo.toml

[dependencies]typename = "0.1.1"
Enter fullscreen mode Exit fullscreen mode

I know that I haven't explained that, but believe me, it's way easier to show the type of data with this method, in another post, I'm going to explain that.

These are all basic types of variables that exist in Rust

Boolean: bool

String: &str

Character: char

Integers:
i8 i16 i32 i64 i128 isize
u8 u16 u32 u64 u128 usize

Float:
f32 f64

Rust does not have NULL, then you can't do something like that:

fn main() {
  let color: &str;
  let number: i8;
  println!("Value: {}", number);
  println!("Value: {}", color);
}
Enter fullscreen mode Exit fullscreen mode

challenge-accepted-let-the-challenge-commence.gif
And again the same challenge, what if?

fn main() {
  let mut number = 1;
  number = "one";
  println!("Value: {}", number);
}
Enter fullscreen mode Exit fullscreen mode

The last thing that you should know, I am sure you noticed semicolon use, is not mandatory in the last line of code, but if you have to write more than one line, you have to use a semicolon.

My recommendation about semicolons is to use them all the time. But if you forget, the language will tell you. 😉

Conclusion

Structure: They are very similar but Golang is easier than Rust because you don't have to use a semicolon or external dependence to print type of value, and Rust for now is not possible to use null or zero value.

Then this time Golang takes a little advantage over Rust.

But I think it is not enough to know who is better.

What do you think? Is the same for you?

I hope you enjoy my post and remember that I am just a Dev like you!

Top comments (0)