1
00:00:00,840 --> 00:00:08,790
[Music]

2
00:00:04,819 --> 00:00:10,650
hi today I'd like to talk about from and

3
00:00:08,790 --> 00:00:13,440
in two traits in rust

4
00:00:10,650 --> 00:00:15,420
those two are widely used and allow us

5
00:00:13,440 --> 00:00:18,119
to implement friendly and economical

6
00:00:15,420 --> 00:00:20,310
api's they can be considered as an

7
00:00:18,119 --> 00:00:24,449
alternative to method overriding which

8
00:00:20,310 --> 00:00:27,180
actually does not exist in rust let us

9
00:00:24,449 --> 00:00:32,040
take a look at implementation of from

10
00:00:27,180 --> 00:00:35,690
trade as you see it's a generic trade

11
00:00:32,040 --> 00:00:38,250
generic or type T that requires

12
00:00:35,690 --> 00:00:44,450
implementation only of a single method

13
00:00:38,250 --> 00:00:48,059
from that takes type T and returns self

14
00:00:44,450 --> 00:00:51,510
this may sound too abstract to you so

15
00:00:48,059 --> 00:00:56,090
let's take a look an example standard

16
00:00:51,510 --> 00:01:02,010
library has implementation of trade from

17
00:00:56,090 --> 00:01:04,949
u8 for type u 16 so we can interpret

18
00:01:02,010 --> 00:01:12,140
this as let's just possibility to obtain

19
00:01:04,949 --> 00:01:16,770
type u 16 from type u 8 if we have value

20
00:01:12,140 --> 00:01:19,380
number 13 encoded as type u 8 we can

21
00:01:16,770 --> 00:01:23,549
convert it without any problems in term

22
00:01:19,380 --> 00:01:25,729
type u 16 and it sounds quite logical if

23
00:01:23,549 --> 00:01:29,610
you have something encoded in one bite

24
00:01:25,729 --> 00:01:35,250
it must be possible to keep it to encode

25
00:01:29,610 --> 00:01:38,070
it in two bytes but it wouldn't be

26
00:01:35,250 --> 00:01:42,329
possible to convert things in opposite

27
00:01:38,070 --> 00:01:45,810
direction so we cannot convert you 16

28
00:01:42,329 --> 00:01:48,689
into u 8 because there's some Wells like

29
00:01:45,810 --> 00:01:51,840
let's say 1000 is it just not possible

30
00:01:48,689 --> 00:01:56,100
to encode in one bite so this straight

31
00:01:51,840 --> 00:01:59,310
just does not exist coming back to the

32
00:01:56,100 --> 00:02:01,950
standard library there a lot of

33
00:01:59,310 --> 00:02:05,850
implementation of trade from installed

34
00:02:01,950 --> 00:02:09,390
library you can see it right here

35
00:02:05,850 --> 00:02:12,810
and you see the list is super long it

36
00:02:09,390 --> 00:02:13,830
just ever since it sounds logical that

37
00:02:12,810 --> 00:02:15,990
make sense that

38
00:02:13,830 --> 00:02:18,600
can be converted from one type into

39
00:02:15,990 --> 00:02:22,350
another without any loss of information

40
00:02:18,600 --> 00:02:27,500
is already implemented in standard

41
00:02:22,350 --> 00:02:27,500
library and we should use it

42
00:02:28,280 --> 00:02:36,750
there are also trade into which works

43
00:02:32,460 --> 00:02:40,380
just very similar to from but in

44
00:02:36,750 --> 00:02:46,110
opposite direction and so it's

45
00:02:40,380 --> 00:02:48,840
reciprocal and you see there's not truly

46
00:02:46,110 --> 00:02:52,890
a huge list of invitations there only

47
00:02:48,840 --> 00:02:56,280
one implementation that actually relies

48
00:02:52,890 --> 00:02:58,320
some trade from for us it just means

49
00:02:56,280 --> 00:03:00,570
that if there's implementation of trade

50
00:02:58,320 --> 00:03:03,270
from then we're getting the

51
00:03:00,570 --> 00:03:10,890
implementation of trade into Joseph for

52
00:03:03,270 --> 00:03:14,070
free and let's take a look at rail code

53
00:03:10,890 --> 00:03:18,090
example so here I have function main

54
00:03:14,070 --> 00:03:20,810
that calls function add and get some and

55
00:03:18,090 --> 00:03:24,150
prints it to standard output and

56
00:03:20,810 --> 00:03:28,700
function and it's extremely simple that

57
00:03:24,150 --> 00:03:34,019
takes two integers add some and returns

58
00:03:28,700 --> 00:03:37,350
integer back and it works but what if we

59
00:03:34,019 --> 00:03:43,610
want to pass something different and you

60
00:03:37,350 --> 00:03:48,200
set it to let's say we want to pass I I

61
00:03:43,610 --> 00:03:51,900
64 because this will not compile because

62
00:03:48,200 --> 00:03:56,060
simcha does not match and one of the

63
00:03:51,900 --> 00:04:01,140
solution in this case is to use some

64
00:03:56,060 --> 00:04:06,060
other type that is more generic and in

65
00:04:01,140 --> 00:04:10,410
our case it would be flawed 64 so we can

66
00:04:06,060 --> 00:04:18,239
assume that we accept our input in flat

67
00:04:10,410 --> 00:04:21,479
64 and return also flawed 64 then of

68
00:04:18,239 --> 00:04:24,979
course we'll have to cast all inputs

69
00:04:21,479 --> 00:04:24,979
into flawed 64 here

70
00:04:25,439 --> 00:04:34,330
and it works but still it's not really

71
00:04:30,520 --> 00:04:37,210
fine because I mean we have to cast

72
00:04:34,330 --> 00:04:40,930
everytime we want to pass data it's not

73
00:04:37,210 --> 00:04:47,069
really cool so that's where we can use

74
00:04:40,930 --> 00:04:50,169
generics let's say we want to accept any

75
00:04:47,069 --> 00:05:05,919
type that can be converted into a flawed

76
00:04:50,169 --> 00:05:08,889
64 so we just we accept everything we

77
00:05:05,919 --> 00:05:13,840
don't care what what really matters that

78
00:05:08,889 --> 00:05:17,650
we just can convert it into F 64 if I

79
00:05:13,840 --> 00:05:19,810
compile this it must fail now because

80
00:05:17,650 --> 00:05:25,749
here we actually need to call method

81
00:05:19,810 --> 00:05:31,270
into to to turn this data to John a and

82
00:05:25,749 --> 00:05:42,819
B into flawed 64 well I have syntax

83
00:05:31,270 --> 00:05:50,349
error here yeah now what yeah of course

84
00:05:42,819 --> 00:05:54,939
we can remove this but it still will not

85
00:05:50,349 --> 00:05:58,089
work because diets are different I mean

86
00:05:54,939 --> 00:06:02,949
input types a and B have different are

87
00:05:58,089 --> 00:06:15,250
different if they would be the same mass

88
00:06:02,949 --> 00:06:18,909
work so we have to define them here as

89
00:06:15,250 --> 00:06:24,689
two different types let's call them G a

90
00:06:18,909 --> 00:06:24,689
and also TB

91
00:06:34,680 --> 00:06:45,880
now we are able to pass two different

92
00:06:37,780 --> 00:06:50,820
types they're treated separately now

93
00:06:45,880 --> 00:06:55,500
this looks a little bit ugly so we can

94
00:06:50,820 --> 00:06:58,500
refactor this I'm just right with where

95
00:06:55,500 --> 00:06:58,500
statement

96
00:07:29,290 --> 00:07:39,100
and we also can write this with trade

97
00:07:34,510 --> 00:07:43,090
from and actually it's quite interesting

98
00:07:39,100 --> 00:07:45,180
so if you want to use trade from we

99
00:07:43,090 --> 00:07:49,510
would write it like this

100
00:07:45,180 --> 00:08:04,720
there's f/64 that is that implements

101
00:07:49,510 --> 00:08:22,060
from 40 a and from 40 B and here it

102
00:08:04,720 --> 00:08:26,280
would be like this which one to use from

103
00:08:22,060 --> 00:08:30,160
or in - doesn't really matter just

104
00:08:26,280 --> 00:08:34,510
whatever works whatever looks better in

105
00:08:30,160 --> 00:08:37,919
particular context thank you for

106
00:08:34,510 --> 00:08:37,919
watching and have a nice day


