API Entry и значения по умолчанию
Удобно обновляйте значения.
«API Entry и значения по умолчанию» — бесплатный урок Learn Rust Coding на CoddyKit. Это урок 2 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Learn Rust Coding, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Learn Rust Coding содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
Why the Entry API?
Often you want to update a value if a key exists, or insert a default if it does not.
Doing this with get plus insert is clumsy. The entry API does it cleanly in one step.
entry Returns a Slot
Calling entry(key) gives you a handle to that key's place in the map.
From that handle you decide what to do: fill it with a default, or modify whatever is there.
use std::collections::HashMap;
fn main() {
let mut m: HashMap<&str, i32> = HashMap::new();
m.entry("a").or_insert(0);
println!("{}", m["a"]);
}or_insert
or_insert(default) inserts the default only if the key is missing, then returns a mutable reference to the value.
If the key already exists, the default is ignored and you get the existing value.
use std::collections::HashMap;
fn main() {
let mut m = HashMap::new();
m.insert("a", 10);
m.entry("a").or_insert(99);
m.entry("b").or_insert(99);
println!("a={}, b={}", m["a"], m["b"]);
}Mutating Through the Reference
Because or_insert returns a mutable reference, you can change the value right away.
Dereference it with * to update in place. This is the heart of counting.
use std::collections::HashMap;
fn main() {
let mut m = HashMap::new();
let count = m.entry("hits").or_insert(0);
*count += 1;
*count += 1;
println!("hits = {}", m["hits"]);
}Counting in a Loop
The classic pattern: for each item, get its slot, default it to zero, and increment.
This tallies occurrences in just one line of logic per item.
use std::collections::HashMap;
fn main() {
let mut counts = HashMap::new();
for c in "hello".chars() {
*counts.entry(c).or_insert(0) += 1;
}
println!("l appears {} times", counts[&'l']);
}or_insert_with
When the default is expensive to build, use or_insert_with. It takes a closure that runs only if the key is missing.
This avoids constructing a value you might never need.
use std::collections::HashMap;
fn main() {
let mut m: HashMap<&str, Vec<i32>> = HashMap::new();
m.entry("nums").or_insert_with(Vec::new).push(1);
m.entry("nums").or_insert_with(Vec::new).push(2);
println!("{:?}", m["nums"]);
}and_modify
and_modify runs a closure on the value only if the key already exists.
Chain it with or_insert to handle both cases: modify if present, otherwise insert a starting value.
use std::collections::HashMap;
fn main() {
let mut m = HashMap::new();
m.insert("x", 5);
m.entry("x").and_modify(|v| *v += 10).or_insert(0);
m.entry("y").and_modify(|v| *v += 10).or_insert(1);
println!("x={}, y={}", m["x"], m["y"]);
}or_default
If the value type has a Default (like 0 for integers or an empty Vec), or_default uses it.
It is a shorthand for or_insert(Default::default()).
use std::collections::HashMap;
fn main() {
let mut m: HashMap<&str, i32> = HashMap::new();
*m.entry("score").or_default() += 7;
println!("score = {}", m["score"]);
}Building Lists Per Key
The entry API shines when grouping items into vectors.
For each key, default to an empty Vec the first time, then push onto whatever vector is there.
use std::collections::HashMap;
fn main() {
let mut groups: HashMap<bool, Vec<i32>> = HashMap::new();
for n in [1, 2, 3, 4] {
groups.entry(n % 2 == 0).or_default().push(n);
}
println!("evens: {:?}", groups[&true]);
}Why Not get + insert?
Doing a separate get then insert looks up the key twice and fights the borrow checker.
The entry API does a single lookup and hands you exactly one safe reference, so it is faster and cleaner.
Putting It Together
Here counting and a default come together to find how many distinct words start with each letter.
Notice the one-line tally pattern in action.
use std::collections::HashMap;
fn main() {
let words = ["ant", "art", "bee"];
let mut by_first: HashMap<char, i32> = HashMap::new();
for w in words {
*by_first.entry(w.chars().next().unwrap()).or_insert(0) += 1;
}
println!("a: {}", by_first[&'a']);
}Quick Check
You want to count chars with one lookup per char.
Recap
The entry API lets you insert-or-update in one lookup.
You used or_insert, or_insert_with, or_default, and and_modify to count items and build lists per key cleanly.
Часто задаваемые вопросы
Урок «API Entry и значения по умолчанию» бесплатный?
Да — полный текст урока «API Entry и значения по умолчанию» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Learn Rust Coding, подпишись на CoddyKit PRO. Курс Learn Rust Coding содержит 4 уроков всего.
Чему я научусь в уроке «API Entry и значения по умолчанию»?
Удобно обновляйте значения. Ты практикуешь Learn Rust Coding с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Learn Rust Coding?
Предыдущий опыт не требуется. Learn Rust Coding на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 2 из 4.
Сколько времени занимает урок «API Entry и значения по умолчанию»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Learn Rust Coding?
Да. Каждый урок Learn Rust Coding включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Создание HashMap
- API Entry и значения по умолчанию
- Работа с HashSet
- Подсчёт и группировка