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