diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 04:40:21 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 04:40:21 +0300 |
| commit | f6cbf2bf76e77c8f64312f573f46cdc7bd434acd (patch) | |
| tree | e1798d4d8533fcbfcda2b269ddf5494147466633 /src/fibonacci | |
| parent | d13f336198c40abacff6996f118040ae73dfbb30 (diff) | |
| download | rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.tar.gz rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.tar.bz2 rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.zip | |
add: 8 projects
variables, shadowing, another_function, branches, loops, fahr2cel,
fibonacci, christmax_song
Diffstat (limited to 'src/fibonacci')
| -rw-r--r-- | src/fibonacci/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/fibonacci/src/main.rs | 36 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/fibonacci/Cargo.toml b/src/fibonacci/Cargo.toml new file mode 100644 index 0000000..15ac334 --- /dev/null +++ b/src/fibonacci/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/fibonacci/src/main.rs b/src/fibonacci/src/main.rs new file mode 100644 index 0000000..e5e973a --- /dev/null +++ b/src/fibonacci/src/main.rs @@ -0,0 +1,36 @@ +use std::io; + +fn main() { + println!("Enter n:"); + + loop { + let mut user_input = String::new(); + + io::stdin() + .read_line(&mut user_input) + .expect("Failed to read line."); + + if user_input.trim() == "q" { + println!("Exiting."); + break; + } + + let n: i32 = match user_input.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + let mut a: u64 = 0; + let mut b: u64 = 1; + + // calc fibonacci[n] + for i in 0..n { + let next: u64 = a.checked_add(b) + .unwrap_or_else(|| panic!("overflow at {}", i)); + a = b; + b = next; + } + + println!("fibonacci[{n}]: {a}"); + } +} |
