----The function calculates the n-th Fibonacci number using for loop
previous: previous number in Fibonacci sequence
current: current number in Fibonacci sequence
temp: contains the current number in the sequence
until the next number is calculated
----Example:
input: 0 1 2 3 4 5 6...
output: 1 1 2 3 5 8 13...
*/
pubfnfib(n:u32)->u32{
letmutprevious:u32=1;
letmutcurrent:u32=1;
for_iin0..(n-1){
lettemp=current;
current=current+previous;
previous=temp;
}
current
}
/*
----The function calculates the n-th Fibonacci number using recursion
----Example:
input: 0 1 2 3 4 5 6...
output: 1 1 2 3 5 8 13...
*/
pubfnfib2(n:u32)->u32{
ifn<=1{
return1;
}
fib2(n-1)+fib2(n-2)
}
Compiling solution v0.1.0 (/tmp/d20201014-2816268-yjt89/solution)
Finished test [unoptimized + debuginfo] target(s) in 1.73s
Running target/debug/deps/solution-ebb42508826ef2b4
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target/debug/deps/solution_test-9e954a53ed808c89
running 1 test
test solution_test::test_full ... FAILED
failures:
---- solution_test::test_full stdout ----
thread 'main' panicked at 'attempt to subtract with overflow', src/lib.rs:23:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
solution_test::test_full
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--test solution_test'
whileindex>0{// for ( ; index > 0; --index) pls.. :D
std::mem::swap(&mutprevious,&mutresult);
result+=previous;
index-=1;
}
result
}
Compiling solution v0.1.0 (/tmp/d20201014-2816268-6dt022/solution)
warning: function is never used: `main`
--> src/lib.rs:1:4
|
1 | fn main() {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:8:16
|
8 | assert_eq!(fib(0), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:9:16
|
9 | assert_eq!(fib(1), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:10:16
|
10 | assert_eq!(fib(2), 1 + 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:11:16
|
11 | assert_eq!(fib(3), 1 + 2);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:12:16
|
12 | assert_eq!(fib(4), 2 + 3);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:13:16
|
13 | assert_eq!(fib(5), 3 + 5);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:14:16
|
14 | assert_eq!(fib(6), 5 + 8);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:15:16
|
15 | assert_eq!(fib(7), 8 + 13);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:16:16
|
16 | assert_eq!(fib(8), 13 + 21);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:17:16
|
17 | assert_eq!(fib(9), 21 + 34);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:18:16
|
18 | assert_eq!(fib(10), 34 + 55);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:19:16
|
19 | assert_eq!(fib(11), 55 + 89);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:20:16
|
20 | assert_eq!(fib(12), 89 + 144);
| ^^^ not found in this scope
warning: unused import: `solution::*`
--> tests/solution_test.rs:4:7
|
4 | use solution::*;
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 13 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0425`.
error: could not compile `solution`.
To learn more, run the command again with --verbose.
stdin().read_line(&mutstring).expect("Did not enter a correct number");
lettrimmed=string.trim();
matchtrimmed.parse::<u32>(){
Ok(number)=>println!("Thanks for proper input: {}, now we will make Fibonacci sequence",number),
Err(..)=>println!("Wrong input, please enter a number: {}",trimmed)
};
letstart:u32=0;
letmutnumber:u32=trimmed.parse::<u32>().unwrap();
number+=1;
letmutresult:u32=0;
forninstart..number+1{
result=fib(n);
}
println!("result = {}",result);
}
fnfib(number:u32)->u32{
ifnumber==0{
returnnumber;
}
ifnumber==1{
returnnumber;
}
fib(number-1)+fib(number-2)
}
Compiling solution v0.1.0 (/tmp/d20201014-2816268-1clgapg/solution)
warning: function is never used: `main`
--> src/lib.rs:1:4
|
1 | fn main() {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function is never used: `fib`
--> src/lib.rs:27:4
|
27 | fn fib(number: u32) -> u32 {
| ^^^
warning: 2 warnings emitted
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:8:16
|
8 | assert_eq!(fib(0), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:9:16
|
9 | assert_eq!(fib(1), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:10:16
|
10 | assert_eq!(fib(2), 1 + 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:11:16
|
11 | assert_eq!(fib(3), 1 + 2);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:12:16
|
12 | assert_eq!(fib(4), 2 + 3);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:13:16
|
13 | assert_eq!(fib(5), 3 + 5);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:14:16
|
14 | assert_eq!(fib(6), 5 + 8);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:15:16
|
15 | assert_eq!(fib(7), 8 + 13);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:16:16
|
16 | assert_eq!(fib(8), 13 + 21);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:17:16
|
17 | assert_eq!(fib(9), 21 + 34);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:18:16
|
18 | assert_eq!(fib(10), 34 + 55);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:19:16
|
19 | assert_eq!(fib(11), 55 + 89);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:20:16
|
20 | assert_eq!(fib(12), 89 + 144);
| ^^^ not found in this scope
warning: unused import: `solution::*`
--> tests/solution_test.rs:4:7
|
4 | use solution::*;
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 13 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0425`.
error: could not compile `solution`.
To learn more, run the command again with --verbose.
Compiling solution v0.1.0 (/tmp/d20201014-2816268-my3kqn/solution)
Finished test [unoptimized + debuginfo] target(s) in 1.01s
Running target/debug/deps/solution-ebb42508826ef2b4
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target/debug/deps/solution_test-9e954a53ed808c89
running 1 test
test solution_test::test_full ... FAILED
failures:
---- solution_test::test_full stdout ----
thread 'main' panicked at 'attempt to add with overflow', src/lib.rs:6:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
solution_test::test_full
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--test solution_test'
Compiling solution v0.1.0 (/tmp/d20201014-2816268-119t3g5/solution)
warning: function is never used: `fib`
--> src/lib.rs:1:4
|
1 | fn fib(n: u32) -> u32 {
| ^^^
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted
warning: function is never used: `fib`
--> src/lib.rs:1:4
|
1 | fn fib(n: u32) -> u32 {
| ^^^
|
= note: `#[warn(dead_code)]` on by default
warning: 1 warning emitted
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:8:16
|
8 | assert_eq!(fib(0), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:9:16
|
9 | assert_eq!(fib(1), 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:10:16
|
10 | assert_eq!(fib(2), 1 + 1);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:11:16
|
11 | assert_eq!(fib(3), 1 + 2);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:12:16
|
12 | assert_eq!(fib(4), 2 + 3);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:13:16
|
13 | assert_eq!(fib(5), 3 + 5);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:14:16
|
14 | assert_eq!(fib(6), 5 + 8);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:15:16
|
15 | assert_eq!(fib(7), 8 + 13);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:16:16
|
16 | assert_eq!(fib(8), 13 + 21);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:17:16
|
17 | assert_eq!(fib(9), 21 + 34);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:18:16
|
18 | assert_eq!(fib(10), 34 + 55);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:19:16
|
19 | assert_eq!(fib(11), 55 + 89);
| ^^^ not found in this scope
error[E0425]: cannot find function `fib` in this scope
--> tests/solution_test.rs:20:16
|
20 | assert_eq!(fib(12), 89 + 144);
| ^^^ not found in this scope
warning: unused import: `solution::*`
--> tests/solution_test.rs:4:7
|
4 | use solution::*;
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 13 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0425`.
error: could not compile `solution`.
To learn more, run the command again with --verbose.