Compound Types

Ultimate Rust Crash Course Primitive Types and Control Flow
2 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

Compound types gather multiple values of other types into one type. The first compound type is the tuple tuples store multiple values of any type. We've seen it before, it looks like this parentheses containing comma separated values. The type annotation is pretty intuitive parentheses surrounding comma separated types. There are two ways to access members of a tuple. The first way is to use dot syntax, also known as a field access expression.

I believe rust uses the dot syntax here instead of square brackets to emphasize that members of tuples are not always the same type. Since a tuples fields have no names, you use their indices starting with zero. The second way to access members of a tuple is all at once we've seen this before, you can use a pattern to D structure and access all the elements of a tuple. Please be aware that tuples currently have a maximum arity of 12 Above which you can technically still use the tuple, but only with limited functionality. arity means how many items are in the tuple. So this tuple type has an arity of four, since the current maximum arity is 12.

You can have a dozen elements in a tuple, but not a baker's dozen, at least not with full functionality. Arrays by contrast, store multiple values of the same type. You can specify them literally with square brackets and commas or with a value and how many you want separated by a semicolon. Note that the type annotation for an array always uses the semicolon form. Even when you specify all the literal values in the array. You index values in an array as you would expect with square brackets.

What you would not expect, however, is that arrays are limited to a size of 32, above which they lose most of their functionality. Arrays live on the stack by default and are fixed size so you will usually use vectors slices of vectors instead of arrays. We'll talk about vectors in a later video. In the next video, we will talk about control flow.

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.