Skip to content

Commit 5844d98

Browse files
authored
refactor (#2)
* init single-number.rs * fix path * minor * add LICENSE.md * update ignore * update * add resource * update readme * fix * refactor
1 parent 0d25862 commit 5844d98

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

kamyu104/src/example.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

kamyu104/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod example;
2-
mod single_number;
1+
mod template;
2+
mod single_number;

kamyu104/src/single_number.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Time: O(n)
22
// Space: O(1)
33

4-
#[allow(dead_code)]
5-
pub fn single_number(nums: Vec<i32>) -> i32 {
6-
nums.iter().fold(0, |acc, &x| acc ^ x)
4+
pub struct Solution {}
5+
6+
impl Solution {
7+
pub fn single_number(nums: Vec<i32>) -> i32 {
8+
nums.iter().fold(0, |acc, &num| acc ^ num)
9+
}
710
}
811

912
#[cfg(test)]
@@ -12,7 +15,7 @@ mod tests {
1215

1316
#[test]
1417
fn test_single_number() {
15-
assert_eq!(single_number(vec![2,2,1]), 1);
16-
assert_eq!(single_number(vec![4,1,2,1,2]), 4);
18+
assert_eq!(Solution::single_number(vec![2, 2, 1]), 1);
19+
assert_eq!(Solution::single_number(vec![4, 1, 2, 1, 2]), 4);
1720
}
18-
}
21+
}

kamyu104/src/template.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub struct Solution {}
2+
3+
impl Solution {
4+
pub fn add(a: i32, b: i32) -> i32 {
5+
// only print when a test fails
6+
println!("{}", a + b);
7+
return a + b;
8+
}
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
// Note this useful idiom: importing names from outer (for mod tests) scope.
14+
use super::*;
15+
16+
#[test]
17+
fn test_add() {
18+
assert_eq!(Solution::add(1, 2), 3);
19+
assert_ne!(Solution::add(1, 2), 4);
20+
}
21+
}

0 commit comments

Comments
 (0)