Skip to content

Commit abceae5

Browse files
committed
updated custom_sum, phantom, sum examples
1 parent 539437e commit abceae5

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ in a controlled repository now.
2222
* `result/`: example of defining and using `Result`
2323
* `top.rs`: top-n list example using generics
2424
* `sum.rs`: generic iterator sum example
25+
* `custom_sum.rs`: sum example with custom `Sum` type
2526
* `addy.rs`: trait def/impl example
2627
* `bno-salad.rs`: trait salad example from Blandy and Orendorff
2728
* `matmul.rs`: example of overloading multiplication for matrices
28-
* `custom_sum.rs`: sum example with custom `Sum` type
2929
* `phantom.rs`: phantom type example

custom_sum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ impl Sum<MyU8> for u16 {
1212
}
1313

1414
fn main() {
15-
let s = vec![MyU8(1), MyU8(2), MyU8(3)].into_iter().sum::<u16>();
15+
let s: u16 = vec![MyU8(1), MyU8(2), MyU8(3)].into_iter().sum();
1616
println!("{}", s);
1717
}

phantom.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ unsafe fn hasher(start: *const u8, nbytes: usize) -> u128 {
1010
let mut result: u128 = 0;
1111
for i in 0..nbytes {
1212
let b = *start.offset(i as isize);
13-
result += b as u128;
13+
result = result.wrapping_add(b as u128);
1414
}
1515
result
1616
}
@@ -26,9 +26,9 @@ impl<T> Hash<T> {
2626
}
2727

2828
fn main() {
29-
let h1 = Hash::hash((0i32,));
29+
let h1 = Hash::hash((0i32, 1i32));
3030
println!("{:?}", h1);
31-
let h2 = Hash::hash((0u32,));
31+
let h2 = Hash::hash(1u64);
3232
println!("{:?}", h2);
3333
// Can't even compare these, since they are of
3434
// different type.

sum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
let s = vec![1u8, 2, 3].into_iter().sum();
2+
let s: u8 = vec![1, 2, 3].into_iter().sum();
33
println!("{}", s);
44
}

0 commit comments

Comments
 (0)