Skip to main content

Command Palette

Search for a command to run...

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

Published
2 min readView as Markdown
S
In search of WhoAmI...

title: My first program using Rust - delving into trait... date: '2000-01-01' tags:

- Uncategorized

window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-H1NMD2NEKT'); [youtube https://www.youtube.com/watch?v=jaNhIXL_RHs?si=OqfMDaY0XPCtRQnE]

चरैवेति, चरैवेति - 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...

More from this blog

Som's Tech World...

150 posts

I am in search of WhoAmI. This is my own world of technologies and software. I believe in the following simple fact of life... To win is no more than this... To rise each time you fall...