Skip to content

grade-school: Expect Option<Vec<String>> from grade; add stub file #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercises/grade-school/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl School {
s
}

pub fn grade(&self, grade: u32) -> Option<&Vec<String>> {
self.grades.get(&grade)
pub fn grade(&self, grade: u32) -> Option<Vec<String>> {
self.grades.get(&grade).map(|v| v.iter().cloned().collect())
}
}
26 changes: 26 additions & 0 deletions exercises/grade-school/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[allow(unused_variables)]

pub struct School {
}

impl School {
pub fn new() -> School {
unimplemented!()
}

pub fn add(&mut self, grade: u32, student: &str) {
unimplemented!()
}

pub fn grades(&self) -> Vec<u32> {
unimplemented!()
}

// If grade returned an `Option<&Vec<String>>`,
// the internal implementation would be forced to keep a `Vec<String>` to lend out.
// By returning an owned vector instead,
// the internal implementation is free to use whatever it chooses.
pub fn grade(&self, grade: u32) -> Option<Vec<String>> {
unimplemented!()
}
}
20 changes: 10 additions & 10 deletions exercises/grade-school/tests/grade-school.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate grade_school as school;

fn stringvec_to_strvec(v: &Vec<String>) -> Vec<&str> {
v.iter().map(|s| s.as_ref()).collect()
fn some_strings(v: Vec<&str>) -> Option<Vec<String>> {
Some(v.iter().map(|s| s.to_string()).collect())
}

#[test]
Expand Down Expand Up @@ -58,8 +58,8 @@ fn test_grade_when_no_students_have_that_grade() {
fn test_grade_for_one_student() {
let mut s = school::School::new();
s.add(2, "Aimee");
assert_eq!(s.grade(2).map(|v| stringvec_to_strvec(v)),
Some(vec!["Aimee"]))
assert_eq!(s.grade(2),
some_strings(vec!["Aimee"]));
}

#[test]
Expand All @@ -69,8 +69,8 @@ fn test_grade_returns_students_sorted_by_name() {
s.add(2, "James");
s.add(2, "Blair");
s.add(2, "Paul");
assert_eq!(s.grade(2).map(|v| stringvec_to_strvec(v)),
Some(vec!["Blair", "James", "Paul"]));
assert_eq!(s.grade(2),
some_strings(vec!["Blair", "James", "Paul"]));
}

#[test]
Expand All @@ -80,8 +80,8 @@ fn test_add_students_to_different_grades() {
s.add(3, "Chelsea");
s.add(7, "Logan");
assert_eq!(s.grades(), vec!(3, 7));
assert_eq!(s.grade(3).map(|v| stringvec_to_strvec(v)),
Some(vec!["Chelsea"]));
assert_eq!(s.grade(7).map(|v| stringvec_to_strvec(v)),
Some(vec!["Logan"]));
assert_eq!(s.grade(3),
some_strings(vec!["Chelsea"]));
assert_eq!(s.grade(7),
some_strings(vec!["Logan"]));
}