Решение на упр.06 задача 1 от Васил Петков

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

Към профила на Васил Петков

Резултати

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

Код

pub struct ImportantExcerpt<'a> {
pub part: &'a str,
}
impl<'a> ImportantExcerpt<'a> {
pub fn level(&self) -> i32 {
3i32
}
pub fn announce_and_return_part(&self, announcement: &str) -> &'a str {
println!("{}", announcement);
self.part
}
}
pub fn first_or_default<'a>(text1: &'a str, text2: &'a str, threshold: usize) -> &'a str {
if text1.len() >= threshold {
text1
} else {
text2
}
}
pub fn first_word(s: &str) -> &str {
s.trim_start().split(' ').next().unwrap()
}
pub fn find_longest_excerpt<'a>(excerpts: &'a [ImportantExcerpt]) -> Option<&'a str> {
excerpts.iter().fold(
None,
|accum: Option<&'a str>, cur: &'a ImportantExcerpt| -> Option<&'a str> {
match accum {
Some(x) => {
if x.len() < cur.part.len() {
Some(cur.part)
} else {
accum
}
}
None => Some(cur.part),
}
},
)
}

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

Compiling solution v0.1.0 (/tmp/d20261119-2321049-1wc05j5/solution)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.38s
     Running unittests src/lib.rs (target/debug/deps/solution-2e550d2f70dd933d)

running 4 tests
test solution_test::test_find_longest ... ok
test solution_test::test_find_longest_2 ... ok
test solution_test::test_first_or_default ... ok
test solution_test::test_first_word ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

Васил качи първо решение на 12.11.2026 19:33 (преди 18 дена)