Перечисления с данными
Связывайте значения с вариантами.
«Перечисления с данными» — бесплатный урок Learn Rust Coding на CoddyKit. Это урок 3 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Learn Rust Coding, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Learn Rust Coding содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
Variants Can Hold Data
So far our variants were just names. In Rust, a variant can also carry data inside parentheses.
This lets a single enum describe both the kind of value and the value itself.
enum Message {
Quit,
Move(i32, i32),
Write(String),
}A Variant with One Value
Put a type in parentheses to attach one piece of data to a variant.
Here Celsius(f64) means the Celsius variant holds a f64 number. You supply that number when you build the value.
enum Temperature {
Celsius(f64),
Fahrenheit(f64),
}
fn main() {
let t = Temperature::Celsius(21.5);
println!("Created a temperature");
}Variants with Several Values
A variant can hold multiple values, separated by commas.
Move(i32, i32) stores two integers, perhaps an x and a y coordinate. You pass both when creating it.
enum Shape {
Circle(f64),
Rectangle(f64, f64),
}
fn main() {
let r = Shape::Rectangle(3.0, 4.0);
println!("Made a rectangle");
}Extracting Data with match
To read the data inside a variant, name it in the pattern. The names become variables you can use in that arm.
Here Shape::Circle(r) binds the stored radius to r.
enum Shape {
Circle(f64),
Rectangle(f64, f64),
}
fn main() {
let s = Shape::Circle(2.0);
match s {
Shape::Circle(r) => println!("radius {}", r),
Shape::Rectangle(w, h) => println!("{} x {}", w, h),
}
}Using the Bound Values
Once data is bound to a name, you can compute with it like any variable.
This program matches a shape and calculates its area, returning the result from match.
enum Shape {
Circle(f64),
Rectangle(f64, f64),
}
fn main() {
let s = Shape::Rectangle(3.0, 4.0);
let area = match s {
Shape::Circle(r) => 3.14 * r * r,
Shape::Rectangle(w, h) => w * h,
};
println!("area = {}", area);
}Different Data per Variant
Each variant can hold a different shape of data, and some can hold none at all.
This flexibility is what makes enums so expressive: one type models many distinct cases.
enum Message {
Quit,
Move(i32, i32),
Write(String),
}Struct-Like Variants
A variant can use named fields with curly braces, just like a struct. This makes the meaning of each field clear.
You build it by naming each field, and match it the same way.
enum Event {
Click { x: i32, y: i32 },
KeyPress(char),
}
fn main() {
let e = Event::Click { x: 10, y: 20 };
match e {
Event::Click { x, y } => println!("click at {},{}", x, y),
Event::KeyPress(c) => println!("key {}", c),
}
}Strings in Variants
Variants can hold owned data like a String. Matching gives you access to the text.
Note: matching by value moves the data out, which is fine here because we use it right away.
enum Message {
Write(String),
Quit,
}
fn main() {
let m = Message::Write(String::from("hello"));
match m {
Message::Write(text) => println!("text: {}", text),
Message::Quit => println!("bye"),
}
}The Standard Option Enum
Rust's built-in Option<T> is an enum with data: Some(T) holds a value and None holds nothing.
It is how Rust represents "maybe a value" without using null. You match it just like your own enums.
fn main() {
let maybe: Option<i32> = Some(5);
match maybe {
Some(n) => println!("got {}", n),
None => println!("nothing"),
}
}Putting It Together
This complete program models temperatures, then converts a Celsius reading to Fahrenheit inside a match.
Try changing the variant or its value and re-running.
enum Temperature {
Celsius(f64),
Fahrenheit(f64),
}
fn main() {
let t = Temperature::Celsius(25.0);
let f = match t {
Temperature::Celsius(c) => c * 9.0 / 5.0 + 32.0,
Temperature::Fahrenheit(f) => f,
};
println!("{} F", f);
}When to Use Data Variants
Reach for data-carrying variants when each case naturally comes with extra information.
- A
Circleneeds a radius. - A
Writemessage needs its text. - A
Moveneeds coordinates.
Bundling the data with the variant keeps related information together and type-safe.
Quick Check
Check your understanding of enums that carry data.
Recap
You learned that enum variants can carry data: a single value, several values, named struct-like fields, or none at all.
You read that data by naming it in a match pattern, like Circle(r). The standard Option type works the same way. Next you will refine matches with guards and bindings.
Часто задаваемые вопросы
Урок «Перечисления с данными» бесплатный?
Да — полный текст урока «Перечисления с данными» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Learn Rust Coding, подпишись на CoddyKit PRO. Курс Learn Rust Coding содержит 4 уроков всего.
Чему я научусь в уроке «Перечисления с данными»?
Связывайте значения с вариантами. Ты практикуешь Learn Rust Coding с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Learn Rust Coding?
Предыдущий опыт не требуется. Learn Rust Coding на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 3 из 4.
Сколько времени занимает урок «Перечисления с данными»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Learn Rust Coding?
Да. Каждый урок Learn Rust Coding включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Создание первого перечисления
- Сопоставление вариантов перечисления
- Перечисления с данными
- Защитные условия и привязки в match