[Music]
hi today I'd like to talk about from and
in two traits in rust
those two are widely used and allow us
to implement friendly and economical
api's they can be considered as an
alternative to method overriding which
actually does not exist in rust let us
take a look at implementation of from
trade as you see it's a generic trade
generic or type T that requires
implementation only of a single method
from that takes type T and returns self
this may sound too abstract to you so
let's take a look an example standard
library has implementation of trade from
u8 for type u 16 so we can interpret
this as let's just possibility to obtain
type u 16 from type u 8 if we have value
number 13 encoded as type u 8 we can
convert it without any problems in term
type u 16 and it sounds quite logical if
you have something encoded in one bite
it must be possible to keep it to encode
it in two bytes but it wouldn't be
possible to convert things in opposite
direction so we cannot convert you 16
into u 8 because there's some Wells like
let's say 1000 is it just not possible
to encode in one bite so this straight
just does not exist coming back to the
standard library there a lot of
implementation of trade from installed
library you can see it right here
and you see the list is super long it
just ever since it sounds logical that
make sense that
can be converted from one type into
another without any loss of information
is already implemented in standard
library and we should use it
there are also trade into which works
just very similar to from but in
opposite direction and so it's
reciprocal and you see there's not truly
a huge list of invitations there only
one implementation that actually relies
some trade from for us it just means
that if there's implementation of trade
from then we're getting the
implementation of trade into Joseph for
free and let's take a look at rail code
example so here I have function main
that calls function add and get some and
prints it to standard output and
function and it's extremely simple that
takes two integers add some and returns
integer back and it works but what if we
want to pass something different and you
set it to let's say we want to pass I I
64 because this will not compile because
simcha does not match and one of the
solution in this case is to use some
other type that is more generic and in
our case it would be flawed 64 so we can
assume that we accept our input in flat
64 and return also flawed 64 then of
course we'll have to cast all inputs
into flawed 64 here
and it works but still it's not really
fine because I mean we have to cast
everytime we want to pass data it's not
really cool so that's where we can use
generics let's say we want to accept any
type that can be converted into a flawed
64 so we just we accept everything we
don't care what what really matters that
we just can convert it into F 64 if I
compile this it must fail now because
here we actually need to call method
into to to turn this data to John a and
B into flawed 64 well I have syntax
error here yeah now what yeah of course
we can remove this but it still will not
work because diets are different I mean
input types a and B have different are
different if they would be the same mass
work so we have to define them here as
two different types let's call them G a
and also TB
now we are able to pass two different
types they're treated separately now
this looks a little bit ugly so we can
refactor this I'm just right with where
statement
and we also can write this with trade
from and actually it's quite interesting
so if you want to use trade from we
would write it like this
there's f/64 that is that implements
from 40 a and from 40 B and here it
would be like this which one to use from
or in - doesn't really matter just
whatever works whatever looks better in
particular context thank you for
watching and have a nice day