Skip to content

Instantly share code, notes, and snippets.

@hogashi

hogashi/main.rs Secret

Created November 17, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hogashi/3d21d52680656277b6d788d721218f4b to your computer and use it in GitHub Desktop.
Save hogashi/3d21d52680656277b6d788d721218f4b to your computer and use it in GitHub Desktop.
programming rust chapter 5 reference
fn factorial(n: usize) -> usize {
(1..n+1).fold(1, |a, b| a * b)
}
fn main() {
let r0 = factorial(6);
assert_eq!(r0 + 1009, 1729);
let r1 = factorial(6);
assert_eq!(r1 + &1009, 1729);
let r2 = factorial(6);
assert_eq!(r2 + &mut 1009, 1729);
let r3 = factorial(6);
assert_eq!(r3 + *(1009), 1729);
let r4 = factorial(6);
assert_eq!(r4 + *(&1009), 1729);
let r5 = factorial(6);
assert_eq!(r5 + *(&mut 1009), 1729);
let r6 = factorial(6);
assert_eq!(*r6 + 1009, 1729);
let r7 = factorial(6);
assert_eq!(*r7 + &1009, 1729);
let r8 = factorial(6);
assert_eq!(*r8 + &mut 1009, 1729);
let r9 = factorial(6);
assert_eq!(*r9 + *(1009), 1729);
let r10 = factorial(6);
assert_eq!(*r10 + *(&1009), 1729);
let r11 = factorial(6);
assert_eq!(*r11 + *(&mut 1009), 1729);
let r12 = &factorial(6);
assert_eq!(r12 + 1009, 1729);
let r13 = &factorial(6);
assert_eq!(r13 + &1009, 1729);
let r14 = &factorial(6);
assert_eq!(r14 + &mut 1009, 1729);
let r15 = &factorial(6);
assert_eq!(r15 + *(1009), 1729);
let r16 = &factorial(6);
assert_eq!(r16 + *(&1009), 1729);
let r17 = &factorial(6);
assert_eq!(r17 + *(&mut 1009), 1729);
let r18 = &factorial(6);
assert_eq!(*r18 + 1009, 1729);
let r19 = &factorial(6);
assert_eq!(*r19 + &1009, 1729);
let r20 = &factorial(6);
assert_eq!(*r20 + &mut 1009, 1729);
let r21 = &factorial(6);
assert_eq!(*r21 + *(1009), 1729);
let r22 = &factorial(6);
assert_eq!(*r22 + *(&1009), 1729);
let r23 = &factorial(6);
assert_eq!(*r23 + *(&mut 1009), 1729);
let r24 = &mut factorial(6);
assert_eq!(r24 + 1009, 1729);
let r25 = &mut factorial(6);
assert_eq!(r25 + &1009, 1729);
let r26 = &mut factorial(6);
assert_eq!(r26 + &mut 1009, 1729);
let r27 = &mut factorial(6);
assert_eq!(r27 + *(1009), 1729);
let r28 = &mut factorial(6);
assert_eq!(r28 + *(&1009), 1729);
let r29 = &mut factorial(6);
assert_eq!(r29 + *(&mut 1009), 1729);
let r30 = &mut factorial(6);
assert_eq!(*r30 + 1009, 1729);
let r31 = &mut factorial(6);
assert_eq!(*r31 + &1009, 1729);
let r32 = &mut factorial(6);
assert_eq!(*r32 + &mut 1009, 1729);
let r33 = &mut factorial(6);
assert_eq!(*r33 + *(1009), 1729);
let r34 = &mut factorial(6);
assert_eq!(*r34 + *(&1009), 1729);
let r35 = &mut factorial(6);
assert_eq!(*r35 + *(&mut 1009), 1729);
}
Compiling ch5 v0.1.0 (file:///home/hogas/rust-rindoku/ch5)
error[E0277]: the trait bound `usize: std::ops::Add<&mut {integer}>` is not satisfied
--> src/main.rs:11:17
|
11 | assert_eq!(r2 + &mut 1009, 1729);
| ^ no implementation for `usize + &mut {integer}`
|
= help: the trait `std::ops::Add<&mut {integer}>` is not implemented for `usize`
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:14:19
|
14 | assert_eq!(r3 + *(1009), 1729);
| ^^^^^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:21:14
|
21 | assert_eq!(*r6 + 1009, 1729);
| ^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:23:14
|
23 | assert_eq!(*r7 + &1009, 1729);
| ^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:25:14
|
25 | assert_eq!(*r8 + &mut 1009, 1729);
| ^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:28:14
|
28 | assert_eq!(*r9 + *(1009), 1729);
| ^^^
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:28:20
|
28 | assert_eq!(*r9 + *(1009), 1729);
| ^^^^^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:30:14
|
30 | assert_eq!(*r10 + *(&1009), 1729);
| ^^^^
error[E0614]: type `usize` cannot be dereferenced
--> src/main.rs:32:14
|
32 | assert_eq!(*r11 + *(&mut 1009), 1729);
| ^^^^
error[E0277]: the trait bound `&usize: std::ops::Add<&mut {integer}>` is not satisfied
--> src/main.rs:39:18
|
39 | assert_eq!(r14 + &mut 1009, 1729);
| ^ no implementation for `&usize + &mut {integer}`
|
= help: the trait `std::ops::Add<&mut {integer}>` is not implemented for `&usize`
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:42:20
|
42 | assert_eq!(r15 + *(1009), 1729);
| ^^^^^^^
error[E0277]: the trait bound `usize: std::ops::Add<&mut {integer}>` is not satisfied
--> src/main.rs:53:19
|
53 | assert_eq!(*r20 + &mut 1009, 1729);
| ^ no implementation for `usize + &mut {integer}`
|
= help: the trait `std::ops::Add<&mut {integer}>` is not implemented for `usize`
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:56:21
|
56 | assert_eq!(*r21 + *(1009), 1729);
| ^^^^^^^
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:63:14
|
63 | assert_eq!(r24 + 1009, 1729);
| ^^^^^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:65:14
|
65 | assert_eq!(r25 + &1009, 1729);
| ^^^^^^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:67:14
|
67 | assert_eq!(r26 + &mut 1009, 1729);
| ^^^^^^^^^^^^^^^
|
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:70:20
|
70 | assert_eq!(r27 + *(1009), 1729);
| ^^^^^^^
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:70:14
|
70 | assert_eq!(r27 + *(1009), 1729);
| ^^^^^^^^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:72:14
|
72 | assert_eq!(r28 + *(&1009), 1729);
| ^^^^^^^^^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0369]: binary operation `+` cannot be applied to type `&mut usize`
--> src/main.rs:74:14
|
74 | assert_eq!(r29 + *(&mut 1009), 1729);
| ^^^^^^^^^^^^^^^^^^
|
= note: this is a reference to a type that `+` can be applied to; you need to dereference this variable once for this operation to work
= note: an implementation of `std::ops::Add` might be missing for `&mut usize`
error[E0277]: the trait bound `usize: std::ops::Add<&mut {integer}>` is not satisfied
--> src/main.rs:81:19
|
81 | assert_eq!(*r32 + &mut 1009, 1729);
| ^ no implementation for `usize + &mut {integer}`
|
= help: the trait `std::ops::Add<&mut {integer}>` is not implemented for `usize`
error[E0614]: type `{integer}` cannot be dereferenced
--> src/main.rs:84:21
|
84 | assert_eq!(*r33 + *(1009), 1729);
| ^^^^^^^
error: aborting due to 22 previous errors
error: Could not compile `ch5`.
To learn more, run the command again with --verbose.
const NOREF : i32 = 0;
const REF : i32 = 1;
const REFMUT: i32 = 2;
fn assert_ref(rfac: &i32, dref: &bool, dnum: &bool, rnum: &i32, count: &i32) {
println!(" let r{} = {}",
count,
match *rfac {
NOREF => "factorial(6);",
REF => "&factorial(6);",
_ => "&mut factorial(6);",
}
);
println!(" assert_eq!({}r{} + {}{}{}, 1729);",
if *dref { "*" } else { "" },
count,
if *dnum { "*(" } else { "" },
match *rnum {
NOREF => "1009",
REF => "&1009",
_ => "&mut 1009",
},
if *dnum { ")" } else { "" }
);
}
fn main() {
let rcond = vec![NOREF, REF, REFMUT];
let dcond = vec![false, true];
let mut count = 0;
println!("fn main() {");
for rfac in &rcond {
for dref in &dcond {
for dnum in &dcond {
for rnum in &rcond {
assert_ref(&rfac, &dref, &dnum, &rnum, &count);
count += 1;
}
println!("");
}
}
}
println!("}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment