From 70564f174a742d4c8ee92b6c72bfd2069ae794bb Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 4 Jan 2023 18:45:30 +0100 Subject: [PATCH] feat(solutions): add `find-closest-number/rust/function` --- .../solutions/rust/function/Cargo.lock | 7 +++ .../solutions/rust/function/Cargo.toml | 6 +++ .../solutions/rust/function/README.md | 3 ++ .../solutions/rust/function/src/main.rs | 46 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 challenges/find-closest-number/solutions/rust/function/Cargo.lock create mode 100644 challenges/find-closest-number/solutions/rust/function/Cargo.toml create mode 100644 challenges/find-closest-number/solutions/rust/function/README.md create mode 100644 challenges/find-closest-number/solutions/rust/function/src/main.rs diff --git a/challenges/find-closest-number/solutions/rust/function/Cargo.lock b/challenges/find-closest-number/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/find-closest-number/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/find-closest-number/solutions/rust/function/Cargo.toml b/challenges/find-closest-number/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/find-closest-number/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/find-closest-number/solutions/rust/function/README.md b/challenges/find-closest-number/solutions/rust/function/README.md new file mode 100644 index 0000000..40b5e46 --- /dev/null +++ b/challenges/find-closest-number/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# find-closest-number/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/find-closest-number/solutions/rust/function/src/main.rs b/challenges/find-closest-number/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..1622a9a --- /dev/null +++ b/challenges/find-closest-number/solutions/rust/function/src/main.rs @@ -0,0 +1,46 @@ +use std::io; + +/// From list of `integers`, get number closest to a given `value`. +pub fn find_closest_number(integers: &[i32], value: i32) -> i32 { + let mut current = integers[0]; + for integer in integers { + if (integer - value).abs() < (current - value).abs() { + current = *integer; + } + } + return current.clone(); +} + +fn main() { + let mut given_number = String::new(); + io::stdin() + .read_line(&mut given_number) + .expect("Failed to read `stdin` line."); + let given_number: i32 = given_number + .trim() + .parse() + .expect("Failed to convert `given_number` as an `i32`."); + + let mut length = String::new(); + io::stdin() + .read_line(&mut length) + .expect("Failed to read `stdin` line."); + let length: usize = length + .trim() + .parse() + .expect("Failed to convert `length` as an `usize`."); + + let mut integers: Vec = Vec::new(); + for _ in 0..length { + let mut integer = String::new(); + io::stdin() + .read_line(&mut integer) + .expect("Failed to read `stdin` line."); + let integer: i32 = integer + .trim() + .parse() + .expect("Failed to convert `integer` as an `i32`."); + integers.push(integer); + } + println!("{}", find_closest_number(&integers, given_number)); +}