X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/8fcdbed310c53f621fba0401399659ed1a1ec446..ab7f9b241429bd675b437d2437799de75d2f409b:/workspace/src/part06.rs diff --git a/workspace/src/part06.rs b/workspace/src/part06.rs deleted file mode 100644 index 15b55d2..0000000 --- a/workspace/src/part06.rs +++ /dev/null @@ -1,65 +0,0 @@ -// Rust-101, Part 06: Copy, Lifetimes -// ================================== - -// We continue to work on our `BigInt`, so we start by importing what we already established. -use part05::BigInt; - -// With `BigInt` being about numbers, we should be able to write a version of `vec_min` -// that computes the minimum of a list of `BigInt`. First, we have to write `min` for `BigInt`. -impl BigInt { - fn min_try1(self, other: Self) -> Self { - debug_assert!(self.test_invariant() && other.test_invariant()); - // Now our assumption of having no trailing zeros comes in handy: - // If the lengths of the two numbers differ, we already know which is larger. - if self.data.len() < other.data.len() { - self - } else if self.data.len() > other.data.len() { - other - } else { - // **Exercise 06.1**: Fill in this code. - unimplemented!() - } - } -} - -// Now we can write `vec_min`. -fn vec_min(v: &Vec) -> Option { - let mut min: Option = None; - // If `v` is a shared borrowed vector, then the default for iterating over it is to call `iter`, the iterator that borrows the elements. - for e in v { - unimplemented!() - } - min -} - -// ## `Copy` types - -use part02::{SomethingOrNothing,Something,Nothing}; -impl Copy for SomethingOrNothing {} - - -// ## Lifetimes - -fn head(v: &Vec) -> Option<&T> { - if v.len() > 0 { - unimplemented!() - } else { - None - } -} -// Technically, we are returning a pointer to the first element. But doesn't that mean that callers have to be -// careful? Imagine `head` would be a C++ function, and we would write the following code. -/* - int foo(std::vector v) { - int *first = head(v); - v.push_back(42); - return *first; - } -*/ -fn rust_foo(mut v: Vec) -> i32 { - let first: Option<&i32> = head(&v); - /* v.push(42); */ - *first.unwrap() -} - -