#Please explain me this concept.

31 messages · Page 1 of 1 (latest)

cinder lagoon
#

well, you can't return a trait. I think you mean that you want to return something that implements that trait

digital jacinth
#

yes i dont really see it being useful in anyway tho

#

or im just dumb

cinder lagoon
#

what kind of programming experience do you have?

digital jacinth
#

in rust not much

#

web dev only

cinder lagoon
#

right, so javascript for example

#

when you use e.g. console.log you don't actually care what the type is, you just want to print it, right?

digital jacinth
#

Yap

cinder lagoon
#

this is an example of being to use different types interchangeably

#

the only thing we really care about is that the object can be printed

digital jacinth
#

so its basically more type safe?

midnight wadi
#

It allows you to "mimic" inheritance

cinder lagoon
#

and your Box<dyn Animal> is one way to do that. it's one way to return an object, but we don't actually care what type it is, just that it implements the Animal trait

midnight wadi
#

^

cinder lagoon
midnight wadi
#

Yeah it's not precise

#

You essentially give your structs a common denominator, by implementing a common trait for them

digital jacinth
#

really interesting

cinder lagoon
#

but, i think the basic question is "why is polymorphism useful and why would i want to use it"

#

and traits are a way to abstract over some property and use types interchangeably

digital jacinth
#

really cool. ive been doing a gui for my company for a while now and im pretty limited with my rust knowledge. i was able to make it functional. hehe thanks for help.

cinder lagoon
#

right, so you might have some kind of Component, Input etc traits

digital jacinth
#

what does the *self do

#

*self is like the pointer stuff in c++ and other languages?

#

uh &x is a pointer heh

#

dereferencing?

cinder lagoon
#

right

#

this is actually a bad example, because you don't actually need to dereference there

digital jacinth
#

aha it works without *

#

thanks