01-Introduction

Traffic lights in real life have three states: red, green, and yellow. When it's red, you need to stop; when yellow, you need to wait; when green, you can proceed.

Data describing traffic light states can be defined using enums.
Different behaviors under different states can be described using pattern matching.

02-Answer to Previous Exercise

  1. Use the readln function to get user input, assign to height and weight, and get the BMI index.
  2. Determine the index range and output prompts.

03-Enums

Defining enum types
When defining an enum, you need to list all its possible values one by one. These values are called enum constructors.
Creating an enum value of an enum type.
Option Type
The Option type is defined as a generic enum type, defined as follows (you just need to know that T in angle brackets is a type parameter. When T is different types, you get different Option types).

04-Pattern Matching

Pattern overview
Constant patterns
Wildcard patterns
Binding patterns
Tuple patterns
Type patterns
Enum patterns

05-Structs

Struct type definitions
Defining struct instances
mut functions

06-Exercise

  1. Combine enums, type matching, and structs to implement a program like this:
    Input an animal type: one of cat, dog, sheep, cow, bird,
    Output the animal's sound, food, and main characteristics.

Programmer Wanfeng focuses on AI programming training. Beginners can start making AI projects after watching his tutorial 《30 Lessons · AI Programming Training Camp》 co-produced with Turing Community.

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!