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
€60.89
List Price:  €86.99
You save:  €26.10
£52.07
List Price:  £74.39
You save:  £22.31
CA$95.66
List Price:  CA$136.66
You save:  CA$41
A$107.87
List Price:  A$154.11
You save:  A$46.23
S$89.96
List Price:  S$128.52
You save:  S$38.56
HK$549.41
List Price:  HK$784.90
You save:  HK$235.49
CHF 57.18
List Price:  CHF 81.69
You save:  CHF 24.51
NOK kr696.88
List Price:  NOK kr995.59
You save:  NOK kr298.70
DKK kr454.18
List Price:  DKK kr648.86
You save:  DKK kr194.67
NZ$116.27
List Price:  NZ$166.11
You save:  NZ$49.83
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,556.39
List Price:  ৳12,223.94
You save:  ৳3,667.54
₹6,053.68
List Price:  ₹8,648.48
You save:  ₹2,594.80
RM297.49
List Price:  RM425
You save:  RM127.51
₦108,117.05
List Price:  ₦154,459.55
You save:  ₦46,342.50
₨19,824.96
List Price:  ₨28,322.58
You save:  ₨8,497.62
฿2,283.77
List Price:  ฿3,262.67
You save:  ฿978.90
₺2,767.39
List Price:  ₺3,953.59
You save:  ₺1,186.19
B$385.16
List Price:  B$550.25
You save:  B$165.09
R1,266.96
List Price:  R1,810.02
You save:  R543.06
Лв118.96
List Price:  Лв169.95
You save:  Лв50.99
₩96,270.46
List Price:  ₩137,535.12
You save:  ₩41,264.66
₪245.25
List Price:  ₪350.37
You save:  ₪105.12
₱4,005.07
List Price:  ₱5,721.77
You save:  ₱1,716.70
¥10,136.24
List Price:  ¥14,480.97
You save:  ¥4,344.72
MX$1,328.99
List Price:  MX$1,898.65
You save:  MX$569.65
QR254.87
List Price:  QR364.12
You save:  QR109.24
P935.34
List Price:  P1,336.27
You save:  P400.92
KSh9,042.70
List Price:  KSh12,918.70
You save:  KSh3,876
E£3,537.81
List Price:  E£5,054.24
You save:  E£1,516.42
ብር9,434.65
List Price:  ብር13,478.65
You save:  ብር4,044
Kz63,827.73
List Price:  Kz91,186.38
You save:  Kz27,358.65
CLP$66,028.30
List Price:  CLP$94,330.18
You save:  CLP$28,301.88
CN¥503.11
List Price:  CN¥718.76
You save:  CN¥215.65
RD$4,143.56
List Price:  RD$5,919.63
You save:  RD$1,776.06
DA9,122.93
List Price:  DA13,033.32
You save:  DA3,910.38
FJ$157.35
List Price:  FJ$224.80
You save:  FJ$67.44
Q537.34
List Price:  Q767.66
You save:  Q230.32
GY$14,631.99
List Price:  GY$20,903.73
You save:  GY$6,271.74
ISK kr8,744.55
List Price:  ISK kr12,492.75
You save:  ISK kr3,748.20
DH637.97
List Price:  DH911.42
You save:  DH273.45
L1,198.56
List Price:  L1,712.31
You save:  L513.74
ден3,743.35
List Price:  ден5,347.88
You save:  ден1,604.52
MOP$565.68
List Price:  MOP$808.15
You save:  MOP$242.47
N$1,248
List Price:  N$1,782.94
You save:  N$534.93
C$2,572.13
List Price:  C$3,674.63
You save:  C$1,102.50
रु9,649.16
List Price:  रु13,785.10
You save:  रु4,135.94
S/252.38
List Price:  S/360.56
You save:  S/108.17
K288.44
List Price:  K412.08
You save:  K123.63
SAR262.58
List Price:  SAR375.13
You save:  SAR112.55
ZK1,703.23
List Price:  ZK2,433.29
You save:  ZK730.06
L306.34
List Price:  L437.65
You save:  L131.30
Kč1,510.94
List Price:  Kč2,158.58
You save:  Kč647.64
Ft24,581.78
List Price:  Ft35,118.33
You save:  Ft10,536.55
SEK kr671.18
List Price:  SEK kr958.87
You save:  SEK kr287.69
ARS$81,364.44
List Price:  ARS$116,239.90
You save:  ARS$34,875.45
Bs485.21
List Price:  Bs693.19
You save:  Bs207.97
COP$289,211.13
List Price:  COP$413,176.46
You save:  COP$123,965.33
₡35,234.35
List Price:  ₡50,336.95
You save:  ₡15,102.59
L1,830.23
List Price:  L2,614.73
You save:  L784.50
₲558,871.53
List Price:  ₲798,422.12
You save:  ₲239,550.59
$U2,868.51
List Price:  $U4,098.05
You save:  $U1,229.54
zł260.38
List Price:  zł372
You save:  zł111.61
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.