Zusammengesetzte Typen
Tupel und Arrays
Zusammengesetzte Typen ist eine kostenlose Learn Rust Coding-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Learn Rust Coding-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Learn Rust Coding-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
What Are Compound Types?
Compound types group multiple values into one. Rust has two primitive compound types:
- Tuples — fixed-size groups of possibly different types.
- Arrays — fixed-size groups of the same type.
fn main() {
let tup = (1, 2.0, 'c');
let arr = [10, 20, 30];
println!("{} {}", tup.0, arr[0]);
}Creating Tuples
A tuple bundles values inside parentheses. Each element can be a different type.
The tuple's type is the combination of its element types.
fn main() {
let person: (&str, u32, f64) = ("Alice", 30, 5.6);
println!("{} is {} years old", person.0, person.1);
}Accessing Tuple Elements
Access tuple elements by index using a dot, starting at 0.
tup.0is the first element.tup.1is the second.
fn main() {
let point = (3, 7);
let x = point.0;
let y = point.1;
println!("x={} y={}", x, y);
}Destructuring Tuples
You can break a tuple into separate variables in one step. This is called destructuring.
fn main() {
let coords = (1.5, 2.5, 3.5);
let (a, b, c) = coords;
println!("{} {} {}", a, b, c);
}The Unit Type
An empty tuple () is called the unit type. It represents 'no meaningful value'.
Functions that return nothing actually return ().
fn main() {
let nothing = ();
println!("unit value is {:?}", nothing);
}Creating Arrays
An array holds multiple values of the same type with a fixed length, written in square brackets.
Its type is [type; length].
fn main() {
let days: [&str; 3] = ["Mon", "Tue", "Wed"];
let numbers = [1, 2, 3, 4, 5];
println!("{} {}", days[1], numbers[4]);
}Array Indexing
Access array elements with square brackets and a zero-based index.
Accessing an out-of-bounds index causes a runtime panic — Rust checks bounds for safety.
fn main() {
let colors = ["red", "green", "blue"];
println!("first {}", colors[0]);
println!("last {}", colors[2]);
}Repeated Initial Values
To fill an array with the same value, use [value; count].
This creates an array of count elements all equal to value.
fn main() {
let zeros = [0; 5];
println!("{:?}", zeros);
println!("length is {}", zeros.len());
}Array Length
The .len() method returns the number of elements in an array. Length is fixed at compile time.
If you need a growable list, use a Vec instead.
fn main() {
let scores = [88, 92, 75, 100];
println!("there are {} scores", scores.len());
}Iterating an Array
Use a for loop to visit each element of an array in order.
fn main() {
let nums = [2, 4, 6, 8];
let mut sum = 0;
for n in nums {
sum += n;
}
println!("sum = {}", sum);
}Tuples vs Arrays
When to use each:
- Use a tuple for a fixed group of different types, like a coordinate plus a label.
- Use an array for a fixed group of the same type, like a list of scores.
fn main() {
let record = ("Bob", [90, 85, 95]);
println!("{} first score {}", record.0, (record.1)[0]);
}Quick Check
Choose the correct statement about Rust compound types.
Recap
Rust's compound types:
- Tuples —
(a, b, c), mixed types, accessed by.0, destructurable. - Arrays —
[a, b, c], same type, fixed length, accessed by[index]. ()is the unit type.
fn main() {
let (name, scores) = ("Cara", [70, 80, 90]);
let mut total = 0;
for s in scores {
total += s;
}
println!("{} total {}", name, total);
}Häufig gestellte Fragen
Ist die Lektion „Zusammengesetzte Typen“ kostenlos?
Ja — der vollständige Text von „Zusammengesetzte Typen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Learn Rust Coding-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Learn Rust Coding-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Zusammengesetzte Typen“?
Tupel und Arrays Du übst Learn Rust Coding mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Learn Rust Coding zu starten?
Keine Vorkenntnisse erforderlich. Learn Rust Coding auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Zusammengesetzte Typen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Learn Rust Coding-Lektion Code schreiben und ausführen?
Ja. Jede Learn Rust Coding-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- let und Mutabilität
- Skalare Typen
- Zusammengesetzte Typen
- Shadowing