From 6b1dea33a690293c894af6a4185f7beb0e7f5b4b Mon Sep 17 00:00:00 2001 From: ZargorNET Date: Mon, 7 Oct 2019 16:02:04 +0200 Subject: [PATCH] Added Rust example --- src/rust/Cargo.toml | 7 +++++++ src/rust/src/main.rs | 21 +++++++++++++++++++++ src/rust/src/tests.rs | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/rust/Cargo.toml create mode 100644 src/rust/src/main.rs create mode 100644 src/rust/src/tests.rs diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml new file mode 100644 index 0000000..33ce170 --- /dev/null +++ b/src/rust/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rust_example" +version = "0.1.0" +edition = "2018" + + +[dependencies] \ No newline at end of file diff --git a/src/rust/src/main.rs b/src/rust/src/main.rs new file mode 100644 index 0000000..e1d2d89 --- /dev/null +++ b/src/rust/src/main.rs @@ -0,0 +1,21 @@ +#[cfg(test)] +mod tests; // INCLUDE TEST FILE + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let language = match args.get(0) { + Some(s) => s, + None => "en" // DEFAULT TO ENGLISH + }; + + println!("{}", hi(&language)); +} + +fn hi(lang: &str) -> String { + match lang { + "en" => "Hello World!".to_owned(), + "de" => "Hallo Welt!".to_owned(), + "dk" => "Hey Verden!".to_owned(), + _ => panic!("language not supported") + } +} \ No newline at end of file diff --git a/src/rust/src/tests.rs b/src/rust/src/tests.rs new file mode 100644 index 0000000..6eb4d9c --- /dev/null +++ b/src/rust/src/tests.rs @@ -0,0 +1,22 @@ +use super::*; + +#[test] +fn test_hi_en() { + assert_eq!(&hi("en"), "Hello World!"); +} + +#[test] +fn test_hi_de() { + assert_eq!(&hi("de"), "Hallo Welt!"); +} + +#[test] +fn test_hi_dk() { + assert_eq!(&hi("dk"), "Hey Verden!"); +} + +#[test] +#[should_panic] +fn test_hi_unknown() { + hi("0101"); +} \ No newline at end of file