Thursday, March 21, 2024

My first program using Rust - delving into trait...


चरैवेति, चरैवेति - Charaiveti, Charaiveti - keep walking...

because the motion is the life - we need to move on to keep the balance of life...


We must not stop.


The movement is essential.


Life is like a bicycle


The moment it stops - the balance goes for a toss


Life is like a stream.


The moment the flow of the stream is lost, all sorts of fungi pollute the water - and the water becomes dirty and ceases to be a potable one.


So, we have to keep moving all throughout our lives - physically - spiritually - and metaphorically - we must not stop the movement - because the


Ultimate Stop comes with Death.


So, here we go...


After C++, Java and Python...


my first program in Rust delving into trait - which is like an interface in Java or an abstract class of C++...


In Rust.... I trust....


Source Code:


trait Animal {

fn make_sound(&self);

fn wag_tail(&self){

println!("I don't have a tail...");

}

}


struct Human{}

impl Animal for Human {

fn make_sound(&self) {

println!("Human is speaking...");

}

}


struct Dog {}


impl Animal for Dog {

fn make_sound(&self) {

println!("Dog barks...");

}

fn wag_tail(&self) {

println!("The dog is waging it's tail");

}

}


fn main() {

let dog = Box::new (Dog {});

dog.make_sound();

dog.wag_tail();

let man = Box::new (Human {});

man.make_sound();

man.wag_tail();

}


The output:


Dog barks...

The dog is waging it's tail

Human is speaking...

I don't have a tail...

No comments: