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.63
List Price:  €85.19
You save:  €25.56
£52.34
List Price:  £74.77
You save:  £22.43
CA$96.42
List Price:  CA$137.76
You save:  CA$41.33
A$105.21
List Price:  A$150.31
You save:  A$45.09
S$90.43
List Price:  S$129.19
You save:  S$38.76
HK$544.61
List Price:  HK$778.05
You save:  HK$233.44
CHF 55.82
List Price:  CHF 79.75
You save:  CHF 23.92
NOK kr709.51
List Price:  NOK kr1,013.63
You save:  NOK kr304.12
DKK kr445.26
List Price:  DKK kr636.11
You save:  DKK kr190.85
NZ$120.57
List Price:  NZ$172.26
You save:  NZ$51.68
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,539.66
List Price:  ৳12,200.05
You save:  ৳3,660.38
₹6,340
List Price:  ₹9,057.54
You save:  ₹2,717.53
RM286.77
List Price:  RM409.68
You save:  RM122.91
₦101,665.37
List Price:  ₦145,242.47
You save:  ₦43,577.10
₨19,583.60
List Price:  ₨27,977.78
You save:  ₨8,394.17
฿2,200.79
List Price:  ฿3,144.12
You save:  ฿943.33
₺2,988.29
List Price:  ₺4,269.18
You save:  ₺1,280.88
B$379.30
List Price:  B$541.88
You save:  B$162.58
R1,181.11
List Price:  R1,687.37
You save:  R506.26
Лв116.54
List Price:  Лв166.49
You save:  Лв49.95
₩103,228.95
List Price:  ₩147,476.25
You save:  ₩44,247.30
₪225.57
List Price:  ₪322.26
You save:  ₪96.68
₱4,137.45
List Price:  ₱5,910.90
You save:  ₱1,773.45
¥10,901.99
List Price:  ¥15,574.94
You save:  ¥4,672.95
MX$1,260.79
List Price:  MX$1,801.20
You save:  MX$540.41
QR254.67
List Price:  QR363.83
You save:  QR109.16
P925.89
List Price:  P1,322.75
You save:  P396.86
KSh9,011.01
List Price:  KSh12,873.43
You save:  KSh3,862.41
E£3,327.32
List Price:  E£4,753.52
You save:  E£1,426.20
ብር10,918.61
List Price:  ብር15,598.69
You save:  ብር4,680.07
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$63,708.39
List Price:  CLP$91,015.89
You save:  CLP$27,307.50
CN¥493.74
List Price:  CN¥705.37
You save:  CN¥211.63
RD$4,468.86
List Price:  RD$6,384.36
You save:  RD$1,915.50
DA9,077.28
List Price:  DA12,968.10
You save:  DA3,890.82
FJ$159
List Price:  FJ$227.15
You save:  FJ$68.15
Q535.22
List Price:  Q764.64
You save:  Q229.41
GY$14,619.51
List Price:  GY$20,885.90
You save:  GY$6,266.39
ISK kr8,846.73
List Price:  ISK kr12,638.73
You save:  ISK kr3,792
DH642.87
List Price:  DH918.43
You save:  DH275.55
L1,181.28
List Price:  L1,687.62
You save:  L506.33
ден3,667.39
List Price:  ден5,239.35
You save:  ден1,571.96
MOP$560.34
List Price:  MOP$800.52
You save:  MOP$240.18
N$1,178.95
List Price:  N$1,684.29
You save:  N$505.33
C$2,571.85
List Price:  C$3,674.23
You save:  C$1,102.37
रु10,111.14
List Price:  रु14,445.11
You save:  रु4,333.96
S/235.26
List Price:  S/336.11
You save:  S/100.84
K298.10
List Price:  K425.87
You save:  K127.77
SAR262.61
List Price:  SAR375.18
You save:  SAR112.56
ZK1,612.47
List Price:  ZK2,303.63
You save:  ZK691.15
L303.46
List Price:  L433.54
You save:  L130.07
Kč1,446.63
List Price:  Kč2,066.70
You save:  Kč620.07
Ft22,956.02
List Price:  Ft32,795.72
You save:  Ft9,839.70
SEK kr648.80
List Price:  SEK kr926.89
You save:  SEK kr278.09
ARS$100,838.09
List Price:  ARS$144,060.59
You save:  ARS$43,222.50
Bs482.84
List Price:  Bs689.81
You save:  Bs206.96
COP$266,135.40
List Price:  COP$380,209.72
You save:  COP$114,074.32
₡34,954.75
List Price:  ₡49,937.50
You save:  ₡14,982.75
L1,839.74
List Price:  L2,628.31
You save:  L788.57
₲469,383.81
List Price:  ₲670,577.05
You save:  ₲201,193.23
$U2,742.46
List Price:  $U3,917.97
You save:  $U1,175.50
zł252.31
List Price:  zł360.46
You save:  zł108.15
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.