projects
/
rust-101.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Changed minimum required rustc version from 1.2 to 1.3.
[rust-101.git]
/
workspace
/
src
/
part07.rs
diff --git
a/workspace/src/part07.rs
b/workspace/src/part07.rs
index 0130637b7a83c853880c9126c2b3287099f62e07..916cb01bc67514492a933ceee01caef41b8ad665 100644
(file)
--- a/
workspace/src/part07.rs
+++ b/
workspace/src/part07.rs
@@
-1,5
+1,3
@@
-// ***Remember to enable/add this part in `main.rs`!***
-
// Rust-101, Part 07: Operator Overloading, Tests, Formatting
// ==========================================================
// Rust-101, Part 07: Operator Overloading, Tests, Formatting
// ==========================================================
@@
-13,14
+11,17
@@
pub trait Minimum {
pub fn vec_min<T: Minimum>(v: &Vec<T>) -> Option<&T> {
let mut min: Option<&T> = None;
for e in v {
pub fn vec_min<T: Minimum>(v: &Vec<T>) -> Option<&T> {
let mut min: Option<&T> = None;
for e in v {
- unimplemented!()
+ min = Some(match min {
+ None => e,
+ Some(n) => n.min(e)
+ });
}
min
}
// **Exercise 07.1**: For our `vec_min` to be usable with `BigInt`, you will have to provide an implementation of
// `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1. You should *not*
}
min
}
// **Exercise 07.1**: For our `vec_min` to be usable with `BigInt`, you will have to provide an implementation of
// `Minimum`. You should be able to pretty much copy the code you wrote for exercise 06.1. You should *not*
-// make any copies!
+// make any copies
of `BigInt`
!
impl Minimum for BigInt {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
unimplemented!()
impl Minimum for BigInt {
fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
unimplemented!()
@@
-38,7
+39,7
@@
impl PartialEq for BigInt {
}
}
-// Now we can compare `BigInt`s. Rust treats `P
ra
tialEq` special in that it is wired to the operator `==`:
+// Now we can compare `BigInt`s. Rust treats `P
ar
tialEq` special in that it is wired to the operator `==`:
fn compare_big_ints() {
let b1 = BigInt::new(13);
let b2 = BigInt::new(37);
fn compare_big_ints() {
let b1 = BigInt::new(13);
let b2 = BigInt::new(37);
@@
-60,7
+61,7
@@
fn test_min() {
// ## Formatting
// ## Formatting
-// All formating is handled by [`std::fmt`](http://doc.rust-lang.org/std/fmt/index.html). I won't explain
+// All formating is handled by [`std::fmt`](http
s
://doc.rust-lang.org/std/fmt/index.html). I won't explain
// all the details, and refer you to the documentation instead.
use std::fmt;
// all the details, and refer you to the documentation instead.
use std::fmt;