diff --git a/challenges/camel-case/README.md b/challenges/camel-case/README.md index 10d0338..560c400 100644 --- a/challenges/camel-case/README.md +++ b/challenges/camel-case/README.md @@ -6,7 +6,7 @@ Created by [@Divlo](https://github.com/Divlo) on 5 July 2020. Write a simple camelCase function for strings. All words (except the first) must have their first letter capitalized without spaces. -**Note :** camelCase is the practice of writing phrases such that each word in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. +**Note:** camelCase is the practice of writing phrases such that each word in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. ## Examples diff --git a/challenges/camel-case/solutions/rust/function/Cargo.lock b/challenges/camel-case/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/camel-case/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/camel-case/solutions/rust/function/Cargo.toml b/challenges/camel-case/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/camel-case/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/camel-case/solutions/rust/function/README.md b/challenges/camel-case/solutions/rust/function/README.md new file mode 100644 index 0000000..6ced017 --- /dev/null +++ b/challenges/camel-case/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# camel-case/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/camel-case/solutions/rust/function/src/main.rs b/challenges/camel-case/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..f678f8b --- /dev/null +++ b/challenges/camel-case/solutions/rust/function/src/main.rs @@ -0,0 +1,32 @@ +use std::io; + +pub fn capitalize(string: &str) -> String { + let mut character = string.chars(); + match character.next() { + None => String::new(), + Some(value) => value.to_uppercase().collect::() + character.as_str(), + } +} + +pub fn camel_case(string: &str) -> String { + let mut result: Vec = Vec::new(); + let words: Vec<&str> = string.trim().split_whitespace().collect(); + for (index, word) in words.iter().enumerate() { + let is_first_word = index == 0; + if is_first_word { + result.push(word.to_string()); + } else { + let word = capitalize(&word); + result.push(word); + } + } + return result.join(""); +} + +fn main() { + let mut string = String::new(); + io::stdin() + .read_line(&mut string) + .expect("Failed to read `stdin` line."); + println!("{}", camel_case(&string)); +}