feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "core04-collections-errors"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,16 @@
|
||||
# 04 — Kolekcje i Obsługa Błędów
|
||||
|
||||
## Zakres
|
||||
* `Vec<T>`, `HashMap<K, V>`, `String`
|
||||
* `Result<T, E>` i operator propagacji błędów `?`
|
||||
* Własne typy błędów (`enum`)
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
1. `word_counts(text: &str) -> HashMap<String, usize>` — policz wystąpienia słów (case-insensitive).
|
||||
2. `parse_positive_i32(input: &str) -> Result<i32, ParsePositiveError>` — sparsuj tekst do `i32` i odrzuć wartości `<= 0`.
|
||||
3. `sum_parsed_positives(inputs: &[&str]) -> Result<i32, ParsePositiveError>` — zsumuj listę tekstów, propagując pierwszy błąd przez `?`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=core04-collections-errors
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum ParsePositiveError {
|
||||
InvalidNumber,
|
||||
NotPositive,
|
||||
}
|
||||
|
||||
pub fn word_counts(text: &str) -> HashMap<String, usize> {
|
||||
todo!("Policz wystąpienia słów w text (lowercase)")
|
||||
}
|
||||
|
||||
pub fn parse_positive_i32(input: &str) -> Result<i32, ParsePositiveError> {
|
||||
todo!(
|
||||
"Sparsuj input jako i32; jeśli błąd parsowania -> InvalidNumber, jeśli <= 0 -> NotPositive"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn sum_parsed_positives(inputs: &[&str]) -> Result<i32, ParsePositiveError> {
|
||||
todo!("Zsumuj wyniki parse_positive_i32 używając operatora ?")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn counts_words_case_insensitively() {
|
||||
let counts = word_counts("Rust rust is fast and RUST is safe");
|
||||
assert_eq!(counts.get("rust"), Some(&3));
|
||||
assert_eq!(counts.get("is"), Some(&2));
|
||||
assert_eq!(counts.get("fast"), Some(&1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_positive_numbers() {
|
||||
assert_eq!(parse_positive_i32("42"), Ok(42));
|
||||
assert_eq!(
|
||||
parse_positive_i32("0"),
|
||||
Err(ParsePositiveError::NotPositive)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_positive_i32("-5"),
|
||||
Err(ParsePositiveError::NotPositive)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_positive_i32("abc"),
|
||||
Err(ParsePositiveError::InvalidNumber)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sums_and_propagates_errors() {
|
||||
assert_eq!(sum_parsed_positives(&["1", "2", "3"]), Ok(6));
|
||||
assert_eq!(
|
||||
sum_parsed_positives(&["1", "0", "3"]),
|
||||
Err(ParsePositiveError::NotPositive)
|
||||
);
|
||||
assert_eq!(
|
||||
sum_parsed_positives(&["1", "oops"]),
|
||||
Err(ParsePositiveError::InvalidNumber)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user