FMI Buzz 2

Краен срок
20.11.2020 17:00
Точки
4

Срокът за предаване на решения е отминал

Шефа от първото домашно се завърна (The Fizz Boss) и този път иска повече performance.

Какъв е тоя вектор? Много място заема в паметта, по-добре да е итератор. И как така ще клонираме низове, да не пишем руби, я връщайте някакви references!

Не можем винаги да връщаме references, защото искаме да връщаме и числа, конвертирани към низове, а има доста такива... Няма къде да ги държим. Но може да използваме std::borrow::Cow, за да държим reference когато можем и own-ната стойност, когато не можем:

use std::borrow::Cow;

pub struct FizzBuzzer {
    labels: [String; 3],
}

impl FizzBuzzer {
    pub fn new(labels: [String; 3]) -> Self {
        FizzBuzzer { labels }
    }

    /// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
    /// каквото още ѝ трябва, за да може да връща резултати.
    pub fn iter(&self) -> FizzBuzzerIter {
        todo!()
    }
}

pub struct FizzBuzzerIter {
    fizzbuzzer: &FizzBuzzer,
    // каквито други полета ви трябват
}

Забележете, че FizzBuzzerIter има reference към някакъв FizzBuzzer. Ако се опитате да компилирате този код досега, ще получите грешка, защото не сме сложили lifetime анотации, които са нужни -- вие трябва да добавите каквито трябва. Свободни сте да добавите каквито други полета искате в тази структура също.

impl Iterator for FizzBuzzerIter {
    type Item = Cow<str>;

    /// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
    /// 1:
    ///
    /// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
    /// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
    /// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
    /// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
    ///
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

Ще трябва да прочетете документацията на std::borrow::Cow, за да разберете как се използва. По-досадното е, че ще трябва и да разберете какви lifetime анотации да сложите, за да може кода да се компилира.

Ето ви пълния тест, който ще използваме, за да оценим задачата: rust-homework/challenge_02/test_full.rs. Необичайно е да пуснем пълния тест, но в случая предизвикателството е да компилирате кода -- ако успеете да го компилирате с минаващ тест, ще си заслужите точките.

Съветваме ви да започнете от това да връщате String от итератора, за по-лесно -- test_basic би трябвало да минава с това даже, стига да закоментирате останалите. Така ще имплементирате итератора на сравнително спокойствие. После можете да смените type Item обратно на Cow и да видите как да преминете към него.

Решения

Андрей
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Андрей

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter { fizzbuzzer: self, current: 0 }
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current: usize,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.current += 1;
if self.current % 15 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[2]))
} else if self.current % 3 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[0]))
} else if self.current % 5 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[1]))
} else {
Some(Cow::from(self.current.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1xcqwfd/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.65s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Теодор Тошков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Теодор Тошков

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: self,
index: 1,
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
index: u64,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
let res = if self.index % 15 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[2]))
} else if self.index % 3 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[0]))
} else if self.index % 5 == 0 {
Some(Cow::from(&self.fizzbuzzer.labels[1]))
} else {
Some(Cow::from(self.index.to_string()))
};
self.index += 1;
res
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1bhclwx/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.74s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Йоана Зелова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Йоана Зелова

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {fizzbuzzer: self, number: 0}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
number: u32,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.number += 1;
if self.number % 15 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
} else if self.number % 3 == 0{
Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
} else if self.number % 5 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
} else {
Some(Cow::Owned(self.number.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1xmqkol/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.78s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Тервел Вълков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Тервел Вълков

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: &self,
i: 1 }
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
i: usize,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
// fn next(&mut self) -> Option<Self::Item> {
// if self.i < 1 {
// None
// } else {
// if self.i % 15 == 0 {
// self.i += 1;
// Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
// } else if self.i % 3 == 0 {
// self.i += 1;
// Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
// } else if self.i % 5 == 0 {
// self.i += 1;
// Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
// } else {
// let temp = self.i;
// self.i += 1;
// Some(Cow::Owned(temp.to_string()))
// }
// }
// }
fn next(&mut self) -> Option<Self::Item> {
if self.i < 1 {
None
} else {
let result: Option<Self::Item>;
if self.i % 15 == 0 {
result = Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]));
} else if self.i % 3 == 0 {
result = Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
} else if self.i % 5 == 0 {
result = Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]));
} else {
result = Some(Cow::Owned(self.i.to_string()));
}
self.i += 1;
result
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1y8amjr/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.68s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Иван Лучев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Лучев

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter { fizzbuzzer: self, current: 0 }
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current: u32,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.current += 1;
if self.current % 15 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
} else if self.current % 3 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
} else if self.current % 5 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
} else {
Some(Cow::Owned(self.current.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-3lgsno/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.71s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Йордан Миронски
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Йордан Миронски

use std::borrow::Cow;
pub struct FizzBuzzer
{
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter(&self) -> FizzBuzzerIter {
return FizzBuzzerIter::new(self);
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
n: u32
// каквито други полета ви трябват
}
impl<'a> FizzBuzzerIter<'a>
{
pub fn new(r: &'a FizzBuzzer) -> Self
{
FizzBuzzerIter{fizzbuzzer: r, n: 0}
}
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
self.n += 1;
if self.n % 3 == 0 && self.n % 5 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]));
}
if self.n % 3 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
}
if self.n % 5 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]));
}
Some(Cow::Owned(self.n.to_string()))
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-16yalhp/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.66s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Мартин Тодоров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Тодоров

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter { fizzbuzzer:self, num:1}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
num: i32,
// каквито други полета ви трябват
}
impl <'a>Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
let mut label = "";
let mut number = String::from("");
if self.num % 15 == 0 {
label = &self.fizzbuzzer.labels[2];
}
else if self.num % 3 == 0 {
label = &self.fizzbuzzer.labels[0];
}
else if self.num % 5 == 0 {
label = &self.fizzbuzzer.labels[1];
}
else {
number = self.num.to_string();
}
self.num += 1;
if label == "" {
Some(Cow::Owned(number))
}
else {
Some(Cow::Borrowed(label))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-90q8lm/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.69s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Биляна Йорданова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Биляна Йорданова

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
let iter = FizzBuzzerIter
{
fizzbuzzer: &self,
index: 0,
};
iter
}
}
pub struct FizzBuzzerIter <'a> {
fizzbuzzer: &'a FizzBuzzer,
index: u32,
}
impl <'a> Iterator for FizzBuzzerIter <'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.index += 1;
let fizz : bool = self.index % 3 == 0;
let buzz : bool = self.index % 5 == 0;
if fizz & buzz
{
return Some(Cow::from(&self.fizzbuzzer.labels[2]))
}
else if fizz
{
return Some(Cow::from(&self.fizzbuzzer.labels[0]))
}
else if buzz
{
return Some(Cow::from(&self.fizzbuzzer.labels[1]))
}
else
{
return Some(Cow::from(self.index.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-m3e7l/solution)
    Finished test [unoptimized + debuginfo] target(s) in 3.67s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Пламен Николов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Пламен Николов

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: self,
current_value: 0,
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current_value: u32,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.current_value += 1;
Some(if self.current_value % 3 == 0 && self.current_value % 5 == 0 {
Cow::Borrowed(&self.fizzbuzzer.labels[2])
} else if self.current_value % 3 == 0 {
Cow::Borrowed(&self.fizzbuzzer.labels[0])
} else if self.current_value % 5 == 0 {
Cow::Borrowed(&self.fizzbuzzer.labels[1])
} else {
Cow::Owned(self.current_value.to_string())
}
)
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-nr30kl/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.65s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Тодор Димов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Тодор Димов

use std::borrow::Cow;
pub struct FizzBuzzer
{
labels: [String; 3],
}
impl FizzBuzzer
{
pub fn new(labels: [String; 3]) -> Self
{
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter
{
FizzBuzzerIter { fizzbuzzer: &self, current_number: 1 }
}
}
pub struct FizzBuzzerIter<'a>
{
fizzbuzzer: &'a FizzBuzzer,
current_number: usize,
}
impl<'b> Iterator for FizzBuzzerIter<'b>
{
type Item = Cow<'b, str>;
fn next(&mut self) -> Option<Self::Item>
{
// can't be after the lambda because REASONS
self.current_number += 1;
// can't be a method of the implementation because fuck you that's why
// so we're forced with a borrowing lambda that compiles only when the planets align
let fizzbuzz = |number: usize| -> Self::Item
{
let div_by3 = number % 3 as usize == 0;
let div_by5 = number % 5 as usize == 0;
if !div_by3 && !div_by5
{
Cow::Owned( number.to_string() )
}
else
{
Cow::Borrowed( &self.fizzbuzzer.labels[ ( div_by5 as usize ) << div_by3 as usize ] )
}
};
// apparently post-increment is too confusing for some people
// and we can't have nice things in order to protect Cletus from himself
Some( fizzbuzz( self.current_number - 1 ) )
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-15m8068/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.00s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Илиян Йорданов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Йорданов

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
use crate::FizzBuzzer;
use std::borrow::Cow;
macro_rules! assert_match {
($expr:expr, $pat:pat) => {
if let $pat = $expr {
// all good
} else {
assert!(false, "Expression {:?} does not match the pattern {:?}", $expr, stringify!($pat));
}
}
}
#[test]
fn test_basic() {
let fizzbuzzer = FizzBuzzer::new([
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
]);
let items: Vec<_> = fizzbuzzer.iter().take(15).collect();
assert_eq!(items, [
"1", "2", "Fizz", "4", "Buzz",
"Fizz", "7", "8", "Fizz", "Buzz",
"11", "Fizz", "13", "14", "Fizzbuzz"
]);
}
#[test]
fn test_cow() {
let fizzbuzzer = FizzBuzzer::new([
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
]);
let mut iter = fizzbuzzer.iter();
assert_match!(iter.next(), Some(Cow::Owned(_))); // "1"
assert_match!(iter.next(), Some(Cow::Owned(_))); // "2"
assert_match!(iter.next(), Some(Cow::Borrowed(_))); // "Fizz"
}
#[test]
fn test_labels() {
let fizzbuzzer = FizzBuzzer::new([
String::from("Four"),
String::from("Seasons"),
String::from("Total Landscaping"),
]);
let items: Vec<_> = fizzbuzzer.iter().take(15).collect();
assert_eq!(items, [
"1", "2", "Four", "4", "Seasons",
"Four", "7", "8", "Four", "Seasons",
"11", "Four", "13", "14", "Total Landscaping"
]);
}
}
use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl<'a> FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter(&'a self) -> FizzBuzzerIter<'a> {
FizzBuzzerIter {
fizzbuzzer: &self,
num: 0,
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
num: u32,
// каквито други полета ви трябват
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
self.num = self.num + 1;
if (self.num%3==0)&&(self.num%5==0) {
Some(Cow::Borrowed(&((*self.fizzbuzzer).labels[2])))
}
else if self.num%3==0 {
Some(Cow::Borrowed(&((*self.fizzbuzzer).labels[0])))
}
else if self.num%5==0 {
Some(Cow::Borrowed(&((*self.fizzbuzzer).labels[1])))
}
else {
Some(Cow::Owned(self.num.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1lbwk9q/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.37s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 4 tests
test tests::it_works ... ok
test tests::test_basic ... ok
test tests::test_cow ... ok
test tests::test_labels ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Георги Гергинов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Гергинов

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: self,
curr_count: 1,
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
curr_count: usize,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
if self.curr_count % 15 == 0 {
self.curr_count += 1;
Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
} else if self.curr_count % 3 == 0 {
self.curr_count += 1;
Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
} else if self.curr_count % 5 == 0 {
self.curr_count += 1;
Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
} else {
self.curr_count += 1;
Some(Cow::Owned((self.curr_count - 1).to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-3f7t6p/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.67s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Цветелин Цецков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Цветелин Цецков

use std::borrow::Cow;
#[derive(Clone)]
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: self,
current_number: 0 as usize,
}
}
}
#[derive(Clone)]
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current_number: usize,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
// Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
self.current_number += 1;
match self.current_number {
fizzbuzz if fizzbuzz % 15 == 0 => Some(Cow::Borrowed(&self.fizzbuzzer.labels[2])),
fizz if fizz % 3 == 0 => Some(Cow::Borrowed(&self.fizzbuzzer.labels[0])),
buzz if buzz % 5 == 0 => Some(Cow::Borrowed(&self.fizzbuzzer.labels[1])),
n => Some(Cow::Owned(n.to_string())),
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1mggv58/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.69s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Деян Горанов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Деян Горанов

use std::borrow::Cow;
fn divides(b: u64, a: u64) -> bool {
a % b == 0
}
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter::new(self)
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
counter: u64,
}
impl<'a> FizzBuzzerIter<'a> {
pub fn new(fizzbuzzer: &'a FizzBuzzer) -> Self {
Self { fizzbuzzer, counter: 0 }
}
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
if self.counter == u64::MAX {
Option::None
} else {
self.counter += 1;
Option::Some(
match (divides(3, self.counter), divides(5, self.counter)) {
(false, false) => Cow::Owned(self.counter.to_string()),
(true , false) => Cow::Borrowed(&self.fizzbuzzer.labels[0]),
(false, true ) => Cow::Borrowed(&self.fizzbuzzer.labels[1]),
(true , true ) => Cow::Borrowed(&self.fizzbuzzer.labels[2]),
}
)
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-19hskj9/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.75s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Борислав Димитров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Борислав Димитров

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: self,
i : 1
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
i: u32
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(& mut self) -> Option<Self::Item> {
self.i = self.i + 1;
if self.i%3==1 && self.i%5!=1 {
Some(Cow::Borrowed(self.fizzbuzzer.labels[0].as_str()))
} else if self.i%3!=1 && self.i%5==1{
Some(Cow::Borrowed(self.fizzbuzzer.labels[1].as_str()))
} else if self.i%3==1 && self.i%5==1{
Some(Cow::Borrowed(self.fizzbuzzer.labels[2].as_str()))
} else {
Some(Cow::Owned((self.i-1).to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-9scfo6/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.73s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Костадин Пеков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Костадин Пеков

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter{fizzbuzzer : self, current : 0 }
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current : u32
// каквито други полета ви трябват
}
impl <'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a,str>;
//type Item = String;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
self.current += 1;
if self.current % 15 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]));
}
else if self.current % 3 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
}
else if self.current % 5 == 0
{
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]));
}
else
{
return Some(Cow::Owned(self.current.to_string()));
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1bee2ck/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.64s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Васил Папукчиев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Васил Папукчиев

use std::borrow::Cow;
pub struct FizzBuzzer {
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter::new(&self)
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
state: u32,
}
impl<'a> FizzBuzzerIter<'a> {
fn new(fizzbuzz: &'a FizzBuzzer) -> Self {
Self { state: 0, fizzbuzzer: fizzbuzz}
}
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.state += 1;
if self.state % 3 == 0 {
if self.state % 5 != 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
}
else {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
}
}
else {
if self.state % 5 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
}
else {
Some(Cow::Owned(self.state.to_string()))
}
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1uf85f5/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.68s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Александър Димитров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Димитров

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter{ fizzbuzzer: &self, num: 0 }
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
num: usize
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.num += 1;
let div_by_3 = self.num % 3 == 0;
let div_by_5 = self.num % 5 == 0;
if div_by_3 && div_by_5 {
Some( Cow::Borrowed( &self.fizzbuzzer.labels[2] ) )
} else if div_by_5 {
Some( Cow::Borrowed( &self.fizzbuzzer.labels[1] ) )
} else if div_by_3 {
Some( Cow::Borrowed( &self.fizzbuzzer.labels[0] ) )
} else {
Some( Cow::Owned( self.num.to_string() ) )
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-tbxsmh/solution)
    Finished test [unoptimized + debuginfo] target(s) in 3.71s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Антонина Ускова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Антонина Ускова

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter::new(self)
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current_number: usize,
}
impl<'a> FizzBuzzerIter<'a> {
fn new(fizzbuzzer: &'a FizzBuzzer) -> Self {
Self {
fizzbuzzer,
current_number: 0,
}
}
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.current_number = self.current_number + 1;
match self.current_number {
i if i % 3 == 0 && i % 5 == 0 =>
Some(Cow::Borrowed(&(self.fizzbuzzer.labels[2]))),
i if i % 3 == 0 =>
Some(Cow::Borrowed(&(self.fizzbuzzer.labels[0]))),
i if i % 5 == 0 =>
Some(Cow::Borrowed(&(self.fizzbuzzer.labels[1]))),
i => Some(Cow::Owned(i.to_string()))
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-kzs73n/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.71s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Ивайло Иванов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ивайло Иванов

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter<'a>(&'a self) -> FizzBuzzerIter {
FizzBuzzerIter{
fizzbuzzer: &self,
index: 1
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
index: usize
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
let result:Option<Self::Item> = match (self.index % 3, self.index % 5) {
(0, 0) => Some(Cow::Borrowed(&self.fizzbuzzer.labels[2])),
(_, 0) => Some(Cow::Borrowed(&self.fizzbuzzer.labels[1])),
(0, _) => Some(Cow::Borrowed(&self.fizzbuzzer.labels[0])),
(_, _) => Some(Cow::Owned(self.index.to_string()))
};
self.index += 1;
result
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-w3jhnt/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.65s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Ралица Димитрова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ралица Димитрова

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter::new(self)
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
n: u32
}
impl<'a> FizzBuzzerIter<'a> {
fn new(fizzbuzzer: &'a FizzBuzzer) -> Self {
Self { fizzbuzzer: fizzbuzzer, n: 0 }
}
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.n = self.n + 1;
if self.n % 3 == 0 && self.n %5 == 0 {
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]));
} else if self.n % 5 == 0 {
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]));
} else if self.n % 3 == 0 {
return Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
} else {
return Some(Cow::Owned(self.n.to_string()));
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-augn77/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.66s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Петър Милев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Милев

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
/// Връщаме нова структура `FizzBuzzerIter`, която има връзка с оригиналния FizzBuzzer и
/// каквото още ѝ трябва, за да може да връща резултати.
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {
fizzbuzzer: &self,
curr_num: 1
}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
curr_num: u8,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
/// Очакваме всяко извикване на тази функция да връща следващото естествено число, започващо от
/// 1:
///
/// - Ако числото се дели на 3, връщаме `Cow::Borrowed`, държащо reference към `labels[0]`
/// - Ако числото се дели на 5, връщаме `Cow::Borrowed`, държащо reference към `labels[1]`
/// - Ако числото се дели на 15, връщаме `Cow::Borrowed`, държащо reference към `labels[2]`
/// - Иначе, връщаме `Cow::Owned`, държащо числото, конвертирано до `String`
///
fn next(&mut self) -> Option<Self::Item> {
let res: Option<Self::Item>;
if self.curr_num % 15 == 0 {
res = Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]));
} else if self.curr_num % 3 == 0 {
res = Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]));
} else if self.curr_num % 5 == 0 {
res = Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]));
} else {
res = Some(Cow::Owned(self.curr_num.to_string()));
}
self.curr_num += 1;
res
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-1k4vvod/solution)
    Finished test [unoptimized + debuginfo] target(s) in 3.73s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Мартин Великов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Великов

use std::borrow::Cow;
pub struct FizzBuzzer {
labels: [String; 3],
}
impl FizzBuzzer {
pub fn new(labels: [String; 3]) -> Self {
FizzBuzzer { labels }
}
pub fn iter(&self) -> FizzBuzzerIter {
FizzBuzzerIter {fizzbuzzer: self, current_num: 0}
}
}
pub struct FizzBuzzerIter<'a> {
fizzbuzzer: &'a FizzBuzzer,
current_num: usize,
}
impl<'a> Iterator for FizzBuzzerIter<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
self.current_num += 1;
if self.current_num % 3 == 0 && self.current_num % 5 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[2]))
} else if self.current_num % 3 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[0]))
} else if self.current_num % 5 == 0 {
Some(Cow::Borrowed(&self.fizzbuzzer.labels[1]))
} else if !(self.current_num % 3 == 0 || self.current_num % 5 == 0 || self.current_num % 15 == 0) {
Some(Cow::Owned(self.current_num.to_string()))
} else {
None
}
}
}
Compiling solution v0.1.0 (/tmp/d20201120-2816268-lwp047/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.87s
     Running target/debug/deps/solution-fd5ff8031b6e4b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-589a43f0f4b10ca3

running 3 tests
test solution_test::test_basic ... ok
test solution_test::test_cow ... ok
test solution_test::test_labels ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out