diff --git a/exercises/features/Cargo.toml b/exercises/features/Cargo.toml new file mode 100644 index 0000000..21a9735 --- /dev/null +++ b/exercises/features/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "features" +version = "0.0.1" +edition = "2018" + +[lib] +name = "features1" +path = "src/features1.rs" + +[features] +english = [] +russian = [] +korean = [] \ No newline at end of file diff --git a/exercises/features/README.md b/exercises/features/README.md new file mode 100644 index 0000000..179509f --- /dev/null +++ b/exercises/features/README.md @@ -0,0 +1,5 @@ +For this exercise check out the sections: +- [Features](https://doc.rust-lang.org/cargo/reference/features.html) +- [Conditional Compilation](https://doc.rust-lang.org/reference/conditional-compilation.html) + +of the Rust Book. diff --git a/exercises/features/src/features1.rs b/exercises/features/src/features1.rs new file mode 100644 index 0000000..a85af4f --- /dev/null +++ b/exercises/features/src/features1.rs @@ -0,0 +1,48 @@ +// features1.rs +// Conditional compilation example: +// `cargo +stable test --features "english" --release` +// `cargo +stable test --features "korean" --release` +// `cargo +stable test --features "russian" --release` +// https://doc.rust-lang.org/cargo/reference/features.html +// https://doc.rust-lang.org/reference/conditional-compilation.html +// Also, try `cargo expand` https://github.com/dtolnay/cargo-expand +// `cargo expand --features "russian"` +// Execute `rustlings hint features1` for hints! + +// I AM NOT DONE + +#[cfg(feature = "korean")] +pub fn hello() -> String { + String::from("안녕하세요!") +} + +#[cfg(feature = "russian")] +pub fn hello() -> String { + String::from("Привет мир!") +} + +#[cfg(feature = "english")] +pub fn hello() -> String { + String::from("Hello World!") +} + +#[cfg(test)] +mod tests { + use crate::hello; + + #[test] + fn say_hello() { + #[cfg(feature = "korean")] + let r = "안녕하세요!".to_string(); + + #[cfg(feature = "english")] + let r = "Hello World!".to_string(); + + #[cfg(feature = "russian")] + let r = "Привет мир!".to_string(); + + let a = hello(); + + assert_eq!(r, a); + } +} diff --git a/info.toml b/info.toml index 2e90f13..5711d95 100644 --- a/info.toml +++ b/info.toml @@ -419,6 +419,15 @@ path = "exercises/quiz4.rs" mode = "test" hint = "No hints this time ;)" +# FEATURES + +[[exercies]] +name = "features1" +path = "exercises/features/src/features1.rs" +mode = "feature" +hint = """TODO: Add some nice hint!""" +features = ["english", "korean", "russian"] + # MOVE SEMANTICS [[exercises]]