Structs

3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€59.89
List Price:  €85.57
You save:  €25.67
£52.68
List Price:  £75.27
You save:  £22.58
CA$96.24
List Price:  CA$137.50
You save:  CA$41.25
A$107.05
List Price:  A$152.93
You save:  A$45.88
S$89.86
List Price:  S$128.38
You save:  S$38.52
HK$545.76
List Price:  HK$779.69
You save:  HK$233.93
CHF 56.96
List Price:  CHF 81.37
You save:  CHF 24.41
NOK kr704.01
List Price:  NOK kr1,005.77
You save:  NOK kr301.76
DKK kr447.07
List Price:  DKK kr638.70
You save:  DKK kr191.63
NZ$118.79
List Price:  NZ$169.71
You save:  NZ$50.92
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,497.01
List Price:  ৳12,139.10
You save:  ৳3,642.09
₹6,170.83
List Price:  ₹8,815.86
You save:  ₹2,645.02
RM295.70
List Price:  RM422.45
You save:  RM126.75
₦107,222.90
List Price:  ₦153,182.14
You save:  ₦45,959.23
₨19,823.65
List Price:  ₨28,320.71
You save:  ₨8,497.06
฿2,264.59
List Price:  ฿3,235.27
You save:  ฿970.67
₺2,876.58
List Price:  ₺4,109.58
You save:  ₺1,233
B$380.14
List Price:  B$543.08
You save:  B$162.94
R1,235.53
List Price:  R1,765.13
You save:  R529.59
Лв117.04
List Price:  Лв167.21
You save:  Лв50.16
₩97,214.01
List Price:  ₩138,883.11
You save:  ₩41,669.10
₪233.25
List Price:  ₪333.23
You save:  ₪99.98
₱3,995.58
List Price:  ₱5,708.22
You save:  ₱1,712.64
¥10,290.97
List Price:  ¥14,702.02
You save:  ¥4,411.05
MX$1,305.67
List Price:  MX$1,865.32
You save:  MX$559.65
QR254.68
List Price:  QR363.84
You save:  QR109.16
P940.72
List Price:  P1,343.95
You save:  P403.22
KSh9,027.58
List Price:  KSh12,897.10
You save:  KSh3,869.51
E£3,357.12
List Price:  E£4,796.09
You save:  E£1,438.97
ብር9,951.53
List Price:  ብር14,217.09
You save:  ብር4,265.55
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$67,622.76
List Price:  CLP$96,608.09
You save:  CLP$28,985.32
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,400.31
List Price:  RD$6,286.43
You save:  RD$1,886.11
DA9,080.64
List Price:  DA12,972.90
You save:  DA3,892.26
FJ$157.85
List Price:  FJ$225.51
You save:  FJ$67.66
Q535.60
List Price:  Q765.18
You save:  Q229.57
GY$14,613.01
List Price:  GY$20,876.62
You save:  GY$6,263.61
ISK kr8,563.48
List Price:  ISK kr12,234.07
You save:  ISK kr3,670.58
DH629.93
List Price:  DH899.93
You save:  DH270
L1,164.15
List Price:  L1,663.14
You save:  L498.99
ден3,684.99
List Price:  ден5,264.49
You save:  ден1,579.50
MOP$561.05
List Price:  MOP$801.54
You save:  MOP$240.48
N$1,240.36
List Price:  N$1,772.03
You save:  N$531.66
C$2,571.43
List Price:  C$3,673.64
You save:  C$1,102.20
रु9,854.29
List Price:  रु14,078.16
You save:  रु4,223.87
S/247.31
List Price:  S/353.31
You save:  S/106
K295.70
List Price:  K422.46
You save:  K126.75
SAR262.63
List Price:  SAR375.20
You save:  SAR112.57
ZK1,648.02
List Price:  ZK2,354.41
You save:  ZK706.39
L303.89
List Price:  L434.15
You save:  L130.26
Kč1,464.79
List Price:  Kč2,092.65
You save:  Kč627.85
Ft23,780.13
List Price:  Ft33,973.07
You save:  Ft10,192.94
SEK kr662.21
List Price:  SEK kr946.06
You save:  SEK kr283.84
ARS$93,215
List Price:  ARS$133,170
You save:  ARS$39,954.99
Bs482.84
List Price:  Bs689.81
You save:  Bs206.96
COP$281,463.10
List Price:  COP$402,107.38
You save:  COP$120,644.27
₡35,283.96
List Price:  ₡50,407.82
You save:  ₡15,123.85
L1,829.53
List Price:  L2,613.72
You save:  L784.19
₲505,187.47
List Price:  ₲721,727.32
You save:  ₲216,539.85
$U2,800.03
List Price:  $U4,000.21
You save:  $U1,200.18
zł255.38
List Price:  zł364.85
You save:  zł109.46
Already have an account? Log In

Transcript

It's time to learn about structs. In other languages, you have classes in rust, you have structs. structs can have data fields, methods and associated functions. The syntax for the struct and its fields is the keyword struct, then the name of the struct in capital, camel case, Redbox. In this case, then curly braces, and then the fields in there type annotations in a list separated by commas, and does a nice touch, you can end your last field with a comma as well, so the compiler doesn't yell at you when you add another field. And don't think to add a comma to the field before it.

This feature is repeated in lots of rust constructs that use commas. instantiating a struct is straightforward, though verbose, you need to specify a value for every single field. Typically, you would implement an associated function to use as a constructor to create a struct with default values and then call that methods and associated functions that are defined in an implementation. block that is separate from this struct definition. The implementation block starts with imple, and then the name of the struct whose functions and methods you're going to implement. This is an associated function, because it doesn't have a form of self as its first parameter, and many other languages, you would call this a class method.

Associated functions are often used to like constructors and other languages with new being the conventional name to use when you want to create a new struct with default values. You'll note that self with a capital S can be used in place of the struct name inside the implementation block. You can also just type out the struct name if you want to, but I recommend using self instead. Let's create our red fox like this. The scope operator in rust is double colons. And we use it to access parts of namespace like things.

We've already used the scope operator in use statements to access items inside of margin. Here we're using it to access an associated function of a struct. Once you have an instantiated value, you get and set fields and call methods with dot syntax as you do in most languages. methods are also defined in the implementation block. Methods always take some form of self is their first argument. At this point in the tutorial for most other languages, I would go for class inheritance for a little bit, or struct inheritance since that's what rust calls its classes.

It's a short discussion, though, because there is no struct inheritance. So wait, is rust an object oriented language or not? Well, it turns out that's a religious war question because there's no universally accepted definition of what an object oriented language is or isn't. And since the rest community doesn't do religious wars, nobody really cares what the answer is. The real question is why doesn't rust have struct inheritance The answer is because they chose a better way to solve the problem we wish inheritance solved, trains which is what we are going to talk about in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.