Решение на упр.06 задача 1 от Андрей Тончев

Обратно към всички решения

Към профила на Андрей Тончев

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)

Код

// Дефинирайте структура ImportantExcerpt<'a>, която пази една имутабилна референция към текстов ред: part: &'a str.
//
// Имплементирайте следните методи за структурата:
// - fn level(&self) -> i32, която връща фиксирано число 3.
// - fn announce_and_return_part(&self, announcement: &str) -> &str, която извежда съобщението announcement на екрана и връща вътрешното поле part.
pub struct ImportantExcerpt<'a> {
pub part: &'a str,
}
impl<'a> ImportantExcerpt<'a> {
pub fn level(&self) -> i32 {
3
}
pub fn announce_and_return_part(&self, announcement: &str) -> &str {
println!("{announcement}");
self.part
}
}
// Напишете функция first_or_default<'a>(text1: &'a str, text2: &'a str, threshold: usize) -> &'a str, която приема два стринга и прагова дължина (threshold).
//
// Логика:
// - Ако дължината на text1 (.len()) е по-голяма или равна на threshold, върнете text1.
// - В противен случай върнете text2.
pub fn first_or_default<'a>(text1: &'a str, text2: &'a str, threshold: usize) -> &'a str {
if text1.len() >= threshold {
text1
} else {
text2
}
}
// Напишете функция first_word<'a>(s: &'a str) -> &'a str, която приема референция към цял текст и връща референция само към неговата първа дума (всичко от началото на стринга до първия интервал ' ').
// Ако в стринга няма интервал, върнете целия входен стринг.
pub fn first_word<'a>(s: &'a str) -> &'a str {
match s.find(' ') {
Some(i) => &s[..i],
None => s,
}
}
// Напишете функция find_longest_excerpt, която приема слайс от структури ImportantExcerpt и връща референция към текста (&str) на онази структура, чието поле part има най-голяма дължина (.len()). Ако слайсът е празен, върнете None.
pub fn find_longest_excerpt<'a>(excerpts: &'a [ImportantExcerpt]) -> Option<&'a str> {
excerpts
.iter()
.reduce(|best, e| if e.part.len() > best.part.len() { e } else { best })
.map(|e| e.part)
}
#[cfg(test)]
mod tests;

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20261119-2321049-1jwvfi5/solution)
error[E0583]: file not found for module `tests`
  --> src/lib.rs:57:1
   |
57 | mod tests;
   | ^^^^^^^^^^
   |
   = help: to create the module `tests`, create file "src/tests.rs" or "src/tests/mod.rs"
   = note: if there is a `mod tests` elsewhere in the crate already, import it with `use crate::...` instead

For more information about this error, try `rustc --explain E0583`.
error: could not compile `solution` (lib test) due to 1 previous error

История (1 версия и 1 коментар)

Андрей качи първо решение на 18.11.2026 20:24 (преди 12 дена)

Имаш компилационна грешка:

Compiling solution v0.1.0 (/tmp/d20261119-2321049-1jwvfi5/solution)
error[E0583]: file not found for module `tests`
  --> src/lib.rs:57:1
   |
57 | mod tests;
   | ^^^^^^^^^^
   |
   = help: to create the module `tests`, create file "src/tests.rs" or "src/tests/mod.rs"
   = note: if there is a `mod tests` elsewhere in the crate already, import it with `use crate::...` instead

For more information about this error, try `rustc --explain E0583`.
error: could not compile `solution` (lib test) due to 1 previous error