Skip to content

Commit 80d2988

Browse files
authored
Merge pull request #454 from petertseng/vecs
Use &[T] instead of &Vec<T> where possible I believe this covers the following Clippy lints: 1. writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used with non-Vec-based slices. * Where this was encountered, the signatures were changed from `Vec<T>` to `&[T]` in one commit; the resulting code compiles. second commit was additionally made to change uses of `vec!` into slices, which is not necessitated by the first commit by the commit but is enabled by it. 2. this argument is passed by value, but not consumed in the function body * Where this was encountered, the signatures were changed from `Vec<T>` to `&[T]` and the usages were changed from `vec!` to slices in one commit, since both changes need to happen together for the tests to compile. 3. useless use of `vec!` * Where this was encountered, uses of `vec!` were changed to slices.
2 parents 77d4388 + 7fd915f commit 80d2988

File tree

11 files changed

+213
-213
lines changed

11 files changed

+213
-213
lines changed

exercises/allergies/tests/allergies.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate allergies;
22

33
use allergies::*;
44

5-
fn compare_allergy_vectors(expected: &Vec<Allergen>, actual: &Vec<Allergen>) {
5+
fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
66
for element in expected {
77
if !actual.contains(element) {
88
panic!("Allergen missing\n {:?} should be in {:?}",
@@ -44,97 +44,97 @@ fn is_allergic_to_egg_shellfish_and_strawberries() {
4444
#[test]
4545
#[ignore]
4646
fn no_allergies_at_all() {
47-
let expected: Vec<Allergen> = Vec::new();
47+
let expected = &[];
4848
let allergies = Allergies::new(0).allergies();
4949

50-
compare_allergy_vectors(&expected, &allergies);
50+
compare_allergy_vectors(expected, &allergies);
5151
}
5252

5353
#[test]
5454
#[ignore]
5555
fn allergic_to_just_eggs() {
56-
let expected = vec![Allergen::Eggs];
56+
let expected = &[Allergen::Eggs];
5757
let allergies = Allergies::new(1).allergies();
5858

59-
compare_allergy_vectors(&expected, &allergies);
59+
compare_allergy_vectors(expected, &allergies);
6060
}
6161

6262
#[test]
6363
#[ignore]
6464
fn allergic_to_just_peanuts() {
65-
let expected = vec![Allergen::Peanuts];
65+
let expected = &[Allergen::Peanuts];
6666
let allergies = Allergies::new(2).allergies();
6767

68-
compare_allergy_vectors(&expected, &allergies);
68+
compare_allergy_vectors(expected, &allergies);
6969
}
7070

7171
#[test]
7272
#[ignore]
7373
fn allergic_to_just_strawberries() {
74-
let expected = vec![Allergen::Strawberries];
74+
let expected = &[Allergen::Strawberries];
7575
let allergies = Allergies::new(8).allergies();
7676

77-
compare_allergy_vectors(&expected, &allergies);
77+
compare_allergy_vectors(expected, &allergies);
7878
}
7979

8080
#[test]
8181
#[ignore]
8282
fn allergic_to_eggs_and_peanuts() {
83-
let expected = vec![Allergen::Eggs, Allergen::Peanuts];
83+
let expected = &[Allergen::Eggs, Allergen::Peanuts];
8484
let allergies = Allergies::new(3).allergies();
8585

86-
compare_allergy_vectors(&expected, &allergies);
86+
compare_allergy_vectors(expected, &allergies);
8787
}
8888

8989
#[test]
9090
#[ignore]
9191
fn allergic_to_eggs_and_shellfish() {
92-
let expected = vec![Allergen::Eggs, Allergen::Shellfish];
92+
let expected = &[Allergen::Eggs, Allergen::Shellfish];
9393
let allergies = Allergies::new(5).allergies();
9494

95-
compare_allergy_vectors(&expected, &allergies);
95+
compare_allergy_vectors(expected, &allergies);
9696
}
9797

9898
#[test]
9999
#[ignore]
100100
fn allergic_to_many_things() {
101-
let expected = vec![Allergen::Strawberries,
102-
Allergen::Tomatoes,
103-
Allergen::Chocolate,
104-
Allergen::Pollen,
105-
Allergen::Cats];
101+
let expected = &[Allergen::Strawberries,
102+
Allergen::Tomatoes,
103+
Allergen::Chocolate,
104+
Allergen::Pollen,
105+
Allergen::Cats];
106106
let allergies = Allergies::new(248).allergies();
107107

108-
compare_allergy_vectors(&expected, &allergies);
108+
compare_allergy_vectors(expected, &allergies);
109109
}
110110

111111
#[test]
112112
#[ignore]
113113
fn allergic_to_everything() {
114-
let expected = vec![Allergen::Eggs,
115-
Allergen::Peanuts,
116-
Allergen::Shellfish,
117-
Allergen::Strawberries,
118-
Allergen::Tomatoes,
119-
Allergen::Chocolate,
120-
Allergen::Pollen,
121-
Allergen::Cats];
114+
let expected = &[Allergen::Eggs,
115+
Allergen::Peanuts,
116+
Allergen::Shellfish,
117+
Allergen::Strawberries,
118+
Allergen::Tomatoes,
119+
Allergen::Chocolate,
120+
Allergen::Pollen,
121+
Allergen::Cats];
122122
let allergies = Allergies::new(255).allergies();
123123

124-
compare_allergy_vectors(&expected, &allergies);
124+
compare_allergy_vectors(expected, &allergies);
125125
}
126126

127127
#[test]
128128
#[ignore]
129129
fn scores_over_255_do_not_trigger_false_positives() {
130-
let expected = vec![Allergen::Eggs,
131-
Allergen::Shellfish,
132-
Allergen::Strawberries,
133-
Allergen::Tomatoes,
134-
Allergen::Chocolate,
135-
Allergen::Pollen,
136-
Allergen::Cats];
130+
let expected = &[Allergen::Eggs,
131+
Allergen::Shellfish,
132+
Allergen::Strawberries,
133+
Allergen::Tomatoes,
134+
Allergen::Chocolate,
135+
Allergen::Pollen,
136+
Allergen::Cats];
137137
let allergies = Allergies::new(509).allergies();
138138

139-
compare_allergy_vectors(&expected, &allergies);
139+
compare_allergy_vectors(expected, &allergies);
140140
}

exercises/dominoes/example.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl AvailabilityTable {
6868
}
6969
}
7070

71-
pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
71+
pub fn chain(dominoes: &[Domino]) -> Option<Vec<Domino>> {
7272
match dominoes.len() {
7373
0 => Some(vec!()),
7474
1 => if dominoes[0].0 == dominoes[0].1 { Some(vec![dominoes[0]]) } else { None },
@@ -100,8 +100,8 @@ pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
100100
}
101101
}
102102

103-
fn chain_worker(dominoes: &Vec<Domino>) -> Vec<Domino> {
104-
let mut doms = dominoes.clone();
103+
fn chain_worker(dominoes: &[Domino]) -> Vec<Domino> {
104+
let mut doms = dominoes.to_vec();
105105
let first = doms.pop().unwrap();
106106
let mut t = AvailabilityTable::new();
107107
for dom in doms.iter() {

exercises/dominoes/tests/dominoes.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn normalize(d: &Domino) -> Domino {
2121
}
2222
}
2323

24-
fn check(input: &Vec<Domino>) -> CheckResult {
24+
fn check(input: &[Domino]) -> CheckResult {
2525
let output = match dominoes::chain(input) {
2626
None => return GotInvalid,
2727
Some(o) => o
@@ -64,7 +64,7 @@ fn check(input: &Vec<Domino>) -> CheckResult {
6464
}
6565
}
6666

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

7777
#[test]
7878
fn empty_input_empty_output() {
79-
let input = vec!();
80-
assert_eq!(dominoes::chain(&input), Some(vec!()));
79+
let input = &[];
80+
assert_eq!(dominoes::chain(input), Some(vec!()));
8181
}
8282

8383
#[test]
8484
#[ignore]
8585
fn singleton_input_singleton_output() {
86-
let input = vec!((1, 1));
87-
assert_correct(&input);
86+
let input = &[(1, 1)];
87+
assert_correct(input);
8888
}
8989

9090
#[test]
9191
#[ignore]
9292
fn singleton_that_cant_be_chained() {
93-
let input = vec![(1, 2)];
94-
assert_eq!(dominoes::chain(&input), None);
93+
let input = &[(1, 2)];
94+
assert_eq!(dominoes::chain(input), None);
9595
}
9696

9797
#[test]
9898
#[ignore]
9999
fn no_repeat_numbers() {
100-
let input = vec!((1, 2), (3, 1), (2, 3));
101-
assert_correct(&input);
100+
let input = &[(1, 2), (3, 1), (2, 3)];
101+
assert_correct(input);
102102
}
103103

104104
#[test]
105105
#[ignore]
106106
fn can_reverse_dominoes() {
107-
let input = vec![(1, 2), (1, 3), (2, 3)];
108-
assert_correct(&input);
107+
let input = &[(1, 2), (1, 3), (2, 3)];
108+
assert_correct(input);
109109
}
110110

111111
#[test]
112112
#[ignore]
113113
fn no_chains() {
114-
let input = vec!((1, 2), (4, 1), (2, 3));
115-
assert_eq!(dominoes::chain(&input), None);
114+
let input = &[(1, 2), (4, 1), (2, 3)];
115+
assert_eq!(dominoes::chain(input), None);
116116
}
117117

118118
#[test]
119119
#[ignore]
120120
fn disconnected_simple() {
121-
let input = vec![(1, 1), (2, 2)];
122-
assert_eq!(dominoes::chain(&input), None);
121+
let input = &[(1, 1), (2, 2)];
122+
assert_eq!(dominoes::chain(input), None);
123123
}
124124

125125
#[test]
126126
#[ignore]
127127
fn disconnected_double_loop() {
128-
let input = vec![(1, 2), (2, 1), (3, 4), (4, 3)];
129-
assert_eq!(dominoes::chain(&input), None);
128+
let input = &[(1, 2), (2, 1), (3, 4), (4, 3)];
129+
assert_eq!(dominoes::chain(input), None);
130130
}
131131

132132
#[test]
133133
#[ignore]
134134
fn disconnected_single_isolated() {
135-
let input = vec![(1, 2), (2, 3), (3, 1), (4, 4)];
136-
assert_eq!(dominoes::chain(&input), None);
135+
let input = &[(1, 2), (2, 3), (3, 1), (4, 4)];
136+
assert_eq!(dominoes::chain(input), None);
137137
}
138138

139139
#[test]
140140
#[ignore]
141141
fn need_backtrack() {
142-
let input = vec![(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)];
143-
assert_correct(&input);
142+
let input = &[(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)];
143+
assert_correct(input);
144144
}
145145

146146
#[test]
147147
#[ignore]
148148
fn separate_loops() {
149-
let input = vec![(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)];
150-
assert_correct(&input);
149+
let input = &[(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)];
150+
assert_correct(input);
151151
}
152152

153153
#[test]
154154
#[ignore]
155155
fn nine_elements() {
156-
let input = vec!((1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6));
157-
assert_correct(&input);
156+
let input = &[(1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6)];
157+
assert_correct(input);
158158
}

exercises/grade-school/tests/grade-school.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate grade_school as school;
22

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

@@ -59,7 +59,7 @@ fn test_grade_for_one_student() {
5959
let mut s = school::School::new();
6060
s.add(2, "Aimee");
6161
assert_eq!(s.grade(2),
62-
some_strings(vec!["Aimee"]));
62+
some_strings(&["Aimee"]));
6363
}
6464

6565
#[test]
@@ -70,7 +70,7 @@ fn test_grade_returns_students_sorted_by_name() {
7070
s.add(2, "Blair");
7171
s.add(2, "Paul");
7272
assert_eq!(s.grade(2),
73-
some_strings(vec!["Blair", "James", "Paul"]));
73+
some_strings(&["Blair", "James", "Paul"]));
7474
}
7575

7676
#[test]
@@ -81,7 +81,7 @@ fn test_add_students_to_different_grades() {
8181
s.add(7, "Logan");
8282
assert_eq!(s.grades(), vec!(3, 7));
8383
assert_eq!(s.grade(3),
84-
some_strings(vec!["Chelsea"]));
84+
some_strings(&["Chelsea"]));
8585
assert_eq!(s.grade(7),
86-
some_strings(vec!["Logan"]));
86+
some_strings(&["Logan"]));
8787
}

0 commit comments

Comments
 (0)