Skip to content

Use &[T] instead of &Vec<T> where possible #454

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 13 commits into from
Mar 13, 2018
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
72 changes: 36 additions & 36 deletions exercises/allergies/tests/allergies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate allergies;

use allergies::*;

fn compare_allergy_vectors(expected: &Vec<Allergen>, actual: &Vec<Allergen>) {
fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
for element in expected {
if !actual.contains(element) {
panic!("Allergen missing\n {:?} should be in {:?}",
Expand Down Expand Up @@ -44,97 +44,97 @@ fn is_allergic_to_egg_shellfish_and_strawberries() {
#[test]
#[ignore]
fn no_allergies_at_all() {
let expected: Vec<Allergen> = Vec::new();
let expected = &[];
let allergies = Allergies::new(0).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_just_eggs() {
let expected = vec![Allergen::Eggs];
let expected = &[Allergen::Eggs];
let allergies = Allergies::new(1).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_just_peanuts() {
let expected = vec![Allergen::Peanuts];
let expected = &[Allergen::Peanuts];
let allergies = Allergies::new(2).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_just_strawberries() {
let expected = vec![Allergen::Strawberries];
let expected = &[Allergen::Strawberries];
let allergies = Allergies::new(8).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_eggs_and_peanuts() {
let expected = vec![Allergen::Eggs, Allergen::Peanuts];
let expected = &[Allergen::Eggs, Allergen::Peanuts];
let allergies = Allergies::new(3).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_eggs_and_shellfish() {
let expected = vec![Allergen::Eggs, Allergen::Shellfish];
let expected = &[Allergen::Eggs, Allergen::Shellfish];
let allergies = Allergies::new(5).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_many_things() {
let expected = vec![Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let expected = &[Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let allergies = Allergies::new(248).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn allergic_to_everything() {
let expected = vec![Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let expected = &[Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let allergies = Allergies::new(255).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}

#[test]
#[ignore]
fn scores_over_255_do_not_trigger_false_positives() {
let expected = vec![Allergen::Eggs,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let expected = &[Allergen::Eggs,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats];
let allergies = Allergies::new(509).allergies();

compare_allergy_vectors(&expected, &allergies);
compare_allergy_vectors(expected, &allergies);
}
6 changes: 3 additions & 3 deletions exercises/dominoes/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl AvailabilityTable {
}
}

pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
pub fn chain(dominoes: &[Domino]) -> Option<Vec<Domino>> {
match dominoes.len() {
0 => Some(vec!()),
1 => if dominoes[0].0 == dominoes[0].1 { Some(vec![dominoes[0]]) } else { None },
Expand Down Expand Up @@ -100,8 +100,8 @@ pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
}
}

fn chain_worker(dominoes: &Vec<Domino>) -> Vec<Domino> {
let mut doms = dominoes.clone();
fn chain_worker(dominoes: &[Domino]) -> Vec<Domino> {
let mut doms = dominoes.to_vec();
let first = doms.pop().unwrap();
let mut t = AvailabilityTable::new();
for dom in doms.iter() {
Expand Down
52 changes: 26 additions & 26 deletions exercises/dominoes/tests/dominoes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn normalize(d: &Domino) -> Domino {
}
}

fn check(input: &Vec<Domino>) -> CheckResult {
fn check(input: &[Domino]) -> CheckResult {
let output = match dominoes::chain(input) {
None => return GotInvalid,
Some(o) => o
Expand Down Expand Up @@ -64,7 +64,7 @@ fn check(input: &Vec<Domino>) -> CheckResult {
}
}

fn assert_correct(input: &Vec<Domino>) {
fn assert_correct(input: &[Domino]) {
match check(&input) {
Correct => (),
GotInvalid => panic!("Unexpectedly got invalid on input {:?}", input),
Expand All @@ -76,83 +76,83 @@ fn assert_correct(input: &Vec<Domino>) {

#[test]
fn empty_input_empty_output() {
let input = vec!();
assert_eq!(dominoes::chain(&input), Some(vec!()));
let input = &[];
assert_eq!(dominoes::chain(input), Some(vec!()));
}

#[test]
#[ignore]
fn singleton_input_singleton_output() {
let input = vec!((1, 1));
assert_correct(&input);
let input = &[(1, 1)];
assert_correct(input);
}

#[test]
#[ignore]
fn singleton_that_cant_be_chained() {
let input = vec![(1, 2)];
assert_eq!(dominoes::chain(&input), None);
let input = &[(1, 2)];
assert_eq!(dominoes::chain(input), None);
}

#[test]
#[ignore]
fn no_repeat_numbers() {
let input = vec!((1, 2), (3, 1), (2, 3));
assert_correct(&input);
let input = &[(1, 2), (3, 1), (2, 3)];
assert_correct(input);
}

#[test]
#[ignore]
fn can_reverse_dominoes() {
let input = vec![(1, 2), (1, 3), (2, 3)];
assert_correct(&input);
let input = &[(1, 2), (1, 3), (2, 3)];
assert_correct(input);
}

#[test]
#[ignore]
fn no_chains() {
let input = vec!((1, 2), (4, 1), (2, 3));
assert_eq!(dominoes::chain(&input), None);
let input = &[(1, 2), (4, 1), (2, 3)];
assert_eq!(dominoes::chain(input), None);
}

#[test]
#[ignore]
fn disconnected_simple() {
let input = vec![(1, 1), (2, 2)];
assert_eq!(dominoes::chain(&input), None);
let input = &[(1, 1), (2, 2)];
assert_eq!(dominoes::chain(input), None);
}

#[test]
#[ignore]
fn disconnected_double_loop() {
let input = vec![(1, 2), (2, 1), (3, 4), (4, 3)];
assert_eq!(dominoes::chain(&input), None);
let input = &[(1, 2), (2, 1), (3, 4), (4, 3)];
assert_eq!(dominoes::chain(input), None);
}

#[test]
#[ignore]
fn disconnected_single_isolated() {
let input = vec![(1, 2), (2, 3), (3, 1), (4, 4)];
assert_eq!(dominoes::chain(&input), None);
let input = &[(1, 2), (2, 3), (3, 1), (4, 4)];
assert_eq!(dominoes::chain(input), None);
}

#[test]
#[ignore]
fn need_backtrack() {
let input = vec![(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)];
assert_correct(&input);
let input = &[(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)];
assert_correct(input);
}

#[test]
#[ignore]
fn separate_loops() {
let input = vec![(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)];
assert_correct(&input);
let input = &[(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)];
assert_correct(input);
}

#[test]
#[ignore]
fn nine_elements() {
let input = vec!((1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6));
assert_correct(&input);
let input = &[(1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6)];
assert_correct(input);
}
10 changes: 5 additions & 5 deletions exercises/grade-school/tests/grade-school.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate grade_school as school;

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

Expand Down Expand Up @@ -59,7 +59,7 @@ fn test_grade_for_one_student() {
let mut s = school::School::new();
s.add(2, "Aimee");
assert_eq!(s.grade(2),
some_strings(vec!["Aimee"]));
some_strings(&["Aimee"]));
}

#[test]
Expand All @@ -70,7 +70,7 @@ fn test_grade_returns_students_sorted_by_name() {
s.add(2, "Blair");
s.add(2, "Paul");
assert_eq!(s.grade(2),
some_strings(vec!["Blair", "James", "Paul"]));
some_strings(&["Blair", "James", "Paul"]));
}

#[test]
Expand All @@ -81,7 +81,7 @@ fn test_add_students_to_different_grades() {
s.add(7, "Logan");
assert_eq!(s.grades(), vec!(3, 7));
assert_eq!(s.grade(3),
some_strings(vec!["Chelsea"]));
some_strings(&["Chelsea"]));
assert_eq!(s.grade(7),
some_strings(vec!["Logan"]));
some_strings(&["Logan"]));
}
Loading