From 2366827a1621cdb75715d74fab2dcd8833a4623a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 27 Feb 2019 10:08:42 -0500 Subject: [PATCH] First commit. Incomplete example iron-gcd. --- .gitignore | 130 +++++++++++++++++++++++++++++++++++++++++++ gcd/Cargo.toml | 7 +++ gcd/src/lib.rs | 32 +++++++++++ gcd/src/main.rs | 26 +++++++++ iron-gcd/Cargo.toml | 11 ++++ iron-gcd/src/main.rs | 30 ++++++++++ 6 files changed, 236 insertions(+) create mode 100644 .gitignore create mode 100644 gcd/Cargo.toml create mode 100644 gcd/src/lib.rs create mode 100644 gcd/src/main.rs create mode 100644 iron-gcd/Cargo.toml create mode 100644 iron-gcd/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea89de5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,130 @@ + +# Created by https://www.gitignore.io/api/rust,macos,jetbrains+all +# Edit at https://www.gitignore.io/?templates=rust,macos,jetbrains+all + +### JetBrains+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### JetBrains+all Patch ### +# Ignores the whole .idea folder and all .iml files +# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 + +.idea/ + +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +# Sonarlint plugin + .idea/sonarlint + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +/target/ +**/target/** + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# End of https://www.gitignore.io/api/rust,macos,jetbrains+all diff --git a/gcd/Cargo.toml b/gcd/Cargo.toml new file mode 100644 index 0000000..1a6280a --- /dev/null +++ b/gcd/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gcd" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] diff --git a/gcd/src/lib.rs b/gcd/src/lib.rs new file mode 100644 index 0000000..cad9dc9 --- /dev/null +++ b/gcd/src/lib.rs @@ -0,0 +1,32 @@ +pub fn gcd(mut n: u64, mut m: u64) -> u64 { + assert!(n != 0 && m != 0); + + while m != 0 { + if m < n { + let t = m; + m = n; + n = t; + } + m = m % n; + } + + n +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_gcd() { + assert_eq!(gcd(14, 15), 1); + + assert_eq!( + gcd( + 2 * 3 * 5 * 11 * 17, + 3 * 7 * 11 * 13 * 19 + ), + 3 * 11 + ) + } +} diff --git a/gcd/src/main.rs b/gcd/src/main.rs new file mode 100644 index 0000000..21e3e49 --- /dev/null +++ b/gcd/src/main.rs @@ -0,0 +1,26 @@ +use gcd::gcd; + +use std::io::Write; +use std::str::FromStr; + +fn main() { + let mut numbers = Vec::new(); + + for arg in std::env::args().skip(1) { + numbers.push( + u64::from_str(&arg).expect("error parsing argument") + ); + } + + if numbers.len() == 0 { + writeln!(std::io::stderr(), "Usage: gcd NUMBER ...").unwrap(); + std::process::exit(1); + } + + let mut d = numbers[0]; + for m in &numbers[1..] { + d = gcd(d, *m); + } + + println!("The greatest common divisor of {:?} is {}", numbers, d); +} diff --git a/iron-gcd/Cargo.toml b/iron-gcd/Cargo.toml new file mode 100644 index 0000000..665ab6f --- /dev/null +++ b/iron-gcd/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "iron-gcd" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] +iron = "0.5.1" +mime = "0.2.3" +router = "0.5.1" +urlencoded = "0.5.0" diff --git a/iron-gcd/src/main.rs b/iron-gcd/src/main.rs new file mode 100644 index 0000000..bd23b6f --- /dev/null +++ b/iron-gcd/src/main.rs @@ -0,0 +1,30 @@ +extern crate iron; +extern crate router; +#[macro_use] extern crate mime; + +use iron::prelude::*; +use iron::status; +use router::Router; + +fn main() { + println!("Serving on http://localhost:3000..."); + + Iron::new(get_form).http("localhost:3000").unwrap(); +} + +fn get_form(_request: &mut Request) -> IronResult { + let mut response = Response::new(); + + response.set_mut(status::Ok); + response.set_mut(mime!(Text/Html; Charset=Utf8)); + response.set_mut(r#" + GCD Calculator +
+ + + +
+ "#); + + Ok(response) +}