Решение на упр.06 задача 1 от Стоян Дрянов
Резултати
- 4 точки от тестове
- 0 бонус точки
- 4 точки общо
- 4 успешни тест(а)
- 0 неуспешни тест(а)
Код
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
}
}
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<'a>(s: &'a str) -> &'a str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[..i];
}
}
s
}
pub fn find_longest_excerpt<'a>(excerpts: &'a [ImportantExcerpt]) -> Option<&'a str> {
let mut longest: Option<&'a str> = None;
for excerpt in excerpts {
match longest {
None => longest = Some(excerpt.part),
Some(current_max) => {
if excerpt.part.len() > current_max.len() {
longest = Some(excerpt.part);
}
}
}
}
longest
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20261119-2321049-8wd1ot/solution)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.35s
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
