diff options
| Commit | c7b48ebf60ed432fc718a922a3c290b6b689a8fa (patch) | |
|---|---|---|
| Author | 2024-10-29 19:29:18 +0200 | |
| Committer | 2024-10-29 19:29:18 +0200 | |
| Files | 3bdf9966253de6a9388649309710ad3248776ce0 | |
| uni-projects-c7b48ebf60ed432fc718a922a3c290b6b689a8fa.tar uni-projects-c7b48ebf60ed432fc718a922a3c290b6b689a8fa.tar.gz uni-projects-c7b48ebf60ed432fc718a922a3c290b6b689a8fa.zip | ||
| Parent | dbdad31081f5fa27f0e1970b7b351d57700ebfc0 (diff) | |
[Rust/hw1] Finished
2 files changed, 18 insertions, 12 deletions
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Year 4/Programming with Rust/hw01/src/lib.rs | 23 |
@@ -329,3 +329,10 @@ TSWLatexianTemp* **/project-files/dynamips **/root/.cache **/root/.mozilla + +# ---- +# Rust +# ---- + +target + diff --git a/Year 4/Programming with Rust/hw01/src/lib.rs b/Year 4/Programming with Rust/hw01/src/lib.rs index 97e921b..ca7845b 100644 --- a/Year 4/Programming with Rust/hw01/src/lib.rs +++ b/Year 4/Programming with Rust/hw01/src/lib.rs @@ -31,28 +31,27 @@ struct Split { count: u32, fib: Option<FibIter>, rfib: Option<RevFibIter>, - n: i64, + n: u32, symmetric: bool, } impl Split { fn new(n: u32, symmetric: bool) -> Split { let mut fib = FibIter::new(); - Split { start: 0, end: fib.inc().unwrap(), count: 0, fib: Option::Some(fib), rfib: Option::None, n: n as i64, symmetric: symmetric } + Split { start: 0, end: fib.inc().unwrap(), count: 0, fib: Option::Some(fib), rfib: Option::None, n: n, symmetric: symmetric } } fn inc(&mut self) { - if self.n == 0 { - self.fib = Option::None; - self.rfib = Option::Some(self.fib.unwrap().rev()); - self.n -= 1; + if self.symmetric && self.n == self.count { + self.rfib = Option::Some(self.fib.take().unwrap().rev()); } self.start = self.end; - self.end += (if self.n > 0 { self.fib.as_mut().unwrap().inc() } else { self.rfib.as_mut().unwrap().inc() }).unwrap(); + self.end += match self.fib { + Some(_) => self. fib.as_mut().unwrap().inc().unwrap(), + None => self.rfib.as_mut().unwrap().inc().unwrap() + }; self.count += 1; - - self.n -= self.symmetric as i64; } } @@ -60,7 +59,7 @@ fn _fib_split(text: &str, n: u32, panic: bool, symmetric: bool) -> (Vec<String>, let mut out: Vec<String> = Vec::new(); let mut sp = Split::new(n, symmetric); - // panic -> sp.count < n + // ( panic -> sp.count < n) while sp.end <= text.len() && (!panic || sp.count < n) { out.push(text[sp.start..sp.end].to_string()); sp.inc(); @@ -92,6 +91,7 @@ impl RevFibIter { return Option::None; } + // 2 3 |-> 1 2 let f0 = self.f0; self.f0 = self.f1 - self.f0; self.f1 = f0; @@ -102,8 +102,8 @@ impl RevFibIter { fn inc(&mut self) -> Option<usize> { let val = self.next(); match val { - Option::None => Option::None, Option::Some(_) => Option::Some(val.unwrap() as usize), + Option::None => Option::None, } } } @@ -112,7 +112,6 @@ pub fn fib_split_n_symmetric(text: &str, n: u32) -> (Vec<String>, &str) { _fib_split(text, n, true, true) } - #[test] fn test_basic() { let mut fib_iter = FibIter::new(); |
