me
Published on

Rust and others

Authors
  • avatar
    Name
    Omer ATAGUN
    Twitter

I had my eyes on Rust for some time, have read the entire documentation one by another and i can not say i am sorry about that. It was more of a novel than a technical documentation and guess what ? i loved it.

In world of many other languages, why rust now ? well actually rust is relatively new, and also elected by stackoverflow survey the most loved one for consecutively 7 years. It is not for just hype. It provides speed, type safety, security and all other beauty along with it.

Plus handles memory like a king.

Everytime i start to learn and get at least basic understanding, i start with an idea. This time it was writing a cli program that can install kubernetes cluster on given machines. I have done it with Rust.

Approach was simple;

Idea

Get some yaml file to know the servers, parse this yaml file, ask for k3s version in arguments, then run bunch of linux based installation for k3s and longhorn, then join nodes to server.

I have gone with this format, so it is going to connect through ss where you also indicate your port for, then do the job for you.


masters:
  - name: "main"
    ip: "127.0.0.1:22"
    username: "root"
    password : "root"
nodes:
  - name: "node1"
    ip: "127.0.0.2:22"
    username: "root"
    password : "root"
  - name: "node2"
    ip: ""
    username: "root"
    password: "something"
  - name: "node3"
    ip: ""
    username: "root"
    password: "something"

If you are interested in checking to see how borrow checkers / mutability works in rust, here, have a look.

Github: Rust Rancher installer

Lets say its partially done, there are a lot more to do but as i feel to, i will be keep adding more stuff onto it. For now, it does exactly what it meant to be.

It was not enough to get this done, i had to do more learning compared to the others. So i have prepared one small sum function that iterates from 1 to 1 million and see the speed of other languages all together.

Looking like this;

Rust

use std::time::{Instant};
fn main() {
    let start = Instant::now();

    let mut sum:i128 = 0;
    for n in 1..1000000 {
        sum += n;
    }

    let finish_time = start.elapsed().as_millis();

    print!("{}", &sum);
}
// After the compilation into binary //
Resulted in 0ms  -> 499999500000

Well, its obvious this is an easy task for rust. Lets take a look at the others.

So i continued and wrote in php, node, python and go

Python

import time

start = time.time()
sum = 0
for X in range(1000000):
    sum += X

end = time.time()
print(sum, end - start)
// Resulted in 60 seconds -> 499999500000

Php

<?php
$sum = 0;
$start = microtime(true);
for($i=1; $i<1000000; $i++){
  $sum += $i;
}
$time_elapsed_secs = microtime(true) - $start;

echo $sum.' seconds: '. $time_elapsed_secs;
// Resulted in 9 seconds - > 499999500000
?>

Node

let sum = 0;
console.time('measure');
for (let i = 1; i < 1000000; i++) {
    sum += i;
}
console.timeEnd('measure')
console.log(sum);

// Resulted in 1 second -> 499999500000

Go

package main


import (
    "fmt"
    "time"
)

func main() {

start := time.Now()

var n, sum = 10000000000, 0
for i := 1; i <= n; i++ {
  sum += i

}
 elapsed := time.Since(start)
fmt.Println("sum - \n", sum)
fmt.Println("elapsed - ", elapsed)
}
// After compilation into binary, resulted in 0 ms -> 499999500000

It was just for a fun to give a look how the others behave in terms of simple sum operation when the number is big and so the allocation is relatively big.

Needless to say that low-level languages have the advantage of doing anything fast but even though, in between go and rust biggest trade off would be the speed of development. Rust is pleasure to work with but way harder to get master at it.

Conclusion

To my taste, i would always prefer to work with rust for any kind of idea or side project in order to learn more and get good at it. I could maybe compliment this series of self education along with go. I have worked aggressively with others either way ( except python )

Go get yourself a rust a little to embrace the beauty of this language and spread this joy into your daily routine time to time. I guarantee you will enjoy what you are doing.