Skip to content

Commit

Permalink
Merge pull request #113 from ferrous-systems/amanjeev/compound-types-…
Browse files Browse the repository at this point in the history
…example

Compound types example fix and linter fixes
  • Loading branch information
jonathanpallant authored Oct 23, 2023
2 parents 52681ab + cc42cff commit bafa6aa
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions training-slides/src/compound-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Point {

## Construction

- there is no partial initialization
- there is no partial initialization

```rust [1-4|6-8]
struct Point {
Expand All @@ -30,7 +30,7 @@ fn main() {

## Construction

- but you can copy from an existing variable of the same type
- but you can copy from an existing variable of the same type

```rust [8]
struct Point {
Expand Down Expand Up @@ -61,8 +61,8 @@ fn main() {

## Tuples

- Holds values of different types together.
- Like an anonymous `struct`, with fields numbered 0, 1, etc.
- Holds values of different types together.
- Like an anonymous `struct`, with fields numbered 0, 1, etc.

```rust [2|3-4]
fn main() {
Expand All @@ -74,9 +74,9 @@ fn main() {

## `()`

- the *empty tuple*
- represents the absence of data
- we often use this similarly to how you’d use `void` in C
- the *empty tuple*
- represents the absence of data
- we often use this similarly to how you’d use `void` in C

```rust
fn prints_but_returns_nothing(data: &str) -> () {
Expand All @@ -86,7 +86,7 @@ fn prints_but_returns_nothing(data: &str) -> () {

## Tuple Structs

- Like a `struct`, with fields numbered 0, 1, etc.
- Like a `struct`, with fields numbered 0, 1, etc.

```rust [1|4|5-6]
struct Point(i32,i32);
Expand All @@ -100,8 +100,8 @@ fn main() {

## Enums

- An `enum` represents different variations of the same subject.
- The different choices in an enum are called *variants*
- An `enum` represents different variations of the same subject.
- The different choices in an enum are called *variants*

<!--
- stress that enums are an "either or" type: you can only have one
Expand Down Expand Up @@ -143,21 +143,21 @@ fn main() {

## Enums with Values

- An enum is the same size, no matter which variant is picked
- It will be the size of the largest variant
- An enum is the same size, no matter which variant is picked
- It will be the size of the largest variant

## Doing a `match` on an `enum`

- When an `enum` has variants, you use `match` to extract the data
- New variables are created from the *pattern* (e.g. `radius`)
- When an `enum` has variants, you use `match` to extract the data
- New variables are created from the *pattern* (e.g. `radius`)

```rust [1-4|7-14|8|11]
enum Shape {
Circle(i32),
Rectangle(i32, i32),
}

fn check_shape(shape: &Shape) {
fn check_shape(shape: Shape) {
match shape {
Shape::Circle(radius) => {
println!("It's a circle, with radius {}", radius);
Expand All @@ -171,16 +171,16 @@ fn check_shape(shape: &Shape) {

## Doing a `match` on an `enum`

- There are two variables called `radius`
- The later one hides the earlier one
- There are two variables called `radius`
- The later one hides the earlier one

```rust [7|9]
enum Shape {
Circle(i32),
Rectangle(i32, i32),
}

fn check_shape(shape: &Shape) {
fn check_shape(shape: Shape) {
let radius = 10;
match shape {
Shape::Circle(radius) => {
Expand All @@ -203,9 +203,9 @@ enum Shape {
Rectangle(i32, i32),
}

fn check_shape(shape: &Shape) {
fn check_shape(shape: Shape) {
match shape {
Shape::Circle(radius) if *radius > 10 => {
Shape::Circle(radius) if radius > 10 => {
println!("It's a BIG circle, with radius {}", radius);
}
_ => {
Expand All @@ -221,7 +221,7 @@ You might ask "Why is there a `*` in front of `radius` in `match`?" - It's becau

## Combining patterns

- You can use the `|` operator to join patterns together
- You can use the `|` operator to join patterns together

```rust [1-16|9]
enum Shape {
Expand All @@ -230,7 +230,7 @@ enum Shape {
Square(i32),
}

fn test_shape(shape: &Shape) {
fn test_shape(shape: Shape) {
match shape {
Shape::Circle(size) | Shape::Square(size) => {
println!("Shape has single size field {}", size);
Expand All @@ -244,16 +244,16 @@ fn test_shape(shape: &Shape) {

## Shorthand: `if let` conditionals

- You can use `if let` if only one case is of interest.
- Still *pattern matching*
- You can use `if let` if only one case is of interest.
- Still *pattern matching*

```rust []
enum Shape {
Circle(i32),
Rectangle(i32, i32),
}

fn test_shape(shape: &Shape) {
fn test_shape(shape: Shape) {
if let Shape::Circle(radius) = shape {
println!("Shape is a Circle with radius {}", radius);
}
Expand All @@ -262,16 +262,16 @@ fn test_shape(shape: &Shape) {

## Shorthand: `let else` conditionals

- If you expect it to match, but want to handle the error...
- The `else` block must *diverge*
- If you expect it to match, but want to handle the error...
- The `else` block must *diverge*

```rust []
enum Shape {
Circle(i32),
Rectangle(i32, i32),
}

fn test_shape(shape: &Shape) {
fn test_shape(shape: Shape) {
let Shape::Circle(radius) = shape else {
println!("I only like circles");
return;
Expand All @@ -282,7 +282,7 @@ fn test_shape(shape: &Shape) {

## Shorthand: `while let` conditionals

- Keep looping whilst the pattern still matches
- Keep looping whilst the pattern still matches

```rust should_panic []
enum Shape {
Expand Down Expand Up @@ -318,4 +318,3 @@ enum Result<T, E> {
```

We'll come back to them after we learn about error handling.

0 comments on commit bafa6aa

Please sign in to comment.