From 1553324b3a8388be7373ddb99e3f4129eff4724d Mon Sep 17 00:00:00 2001 From: Divlo Date: Thu, 5 Jan 2023 18:34:25 +0100 Subject: [PATCH] feat(solutions): add `fibonacci/rust/function` --- challenges/fibonacci/README.md | 2 +- .../solutions/rust/function/Cargo.lock | 7 +++++++ .../solutions/rust/function/Cargo.toml | 6 ++++++ .../solutions/rust/function/README.md | 3 +++ .../solutions/rust/function/src/main.rs | 20 +++++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 challenges/fibonacci/solutions/rust/function/Cargo.lock create mode 100644 challenges/fibonacci/solutions/rust/function/Cargo.toml create mode 100644 challenges/fibonacci/solutions/rust/function/README.md create mode 100644 challenges/fibonacci/solutions/rust/function/src/main.rs diff --git a/challenges/fibonacci/README.md b/challenges/fibonacci/README.md index ecd1278..9a75252 100644 --- a/challenges/fibonacci/README.md +++ b/challenges/fibonacci/README.md @@ -6,7 +6,7 @@ Created by [@Divlo](https://github.com/Divlo) on 5 July 2020. The function should return an array of fibonacci numbers. The function takes a `number` as an argument to decide how many number of elements to produce. -**Note :** The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Each subsequent number is the sum of the previous two. +**Note:** The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Each subsequent number is the sum of the previous two. ## Examples diff --git a/challenges/fibonacci/solutions/rust/function/Cargo.lock b/challenges/fibonacci/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/fibonacci/solutions/rust/function/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_application" +version = "1.0.0" diff --git a/challenges/fibonacci/solutions/rust/function/Cargo.toml b/challenges/fibonacci/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/fibonacci/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/fibonacci/solutions/rust/function/README.md b/challenges/fibonacci/solutions/rust/function/README.md new file mode 100644 index 0000000..3de0511 --- /dev/null +++ b/challenges/fibonacci/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# fibonacci/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 5 January 2023. diff --git a/challenges/fibonacci/solutions/rust/function/src/main.rs b/challenges/fibonacci/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..df3651b --- /dev/null +++ b/challenges/fibonacci/solutions/rust/function/src/main.rs @@ -0,0 +1,20 @@ +use std::io; + +pub fn fibonacci(number: u32) -> u32 { + if number == 0 || number == 1 { + return number; + } + return fibonacci(number - 1) + fibonacci(number - 2); +} + +fn main() { + let mut number = String::new(); + io::stdin() + .read_line(&mut number) + .expect("Failed to read `stdin` line."); + let number: u32 = number + .trim() + .parse() + .expect("Failed to convert `number` as an `u32`."); + println!("{}", fibonacci(number)); +}