diff --git a/challenges/slugify/solutions/rust/function/Cargo.lock b/challenges/slugify/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/slugify/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/slugify/solutions/rust/function/Cargo.toml b/challenges/slugify/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/slugify/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/slugify/solutions/rust/function/README.md b/challenges/slugify/solutions/rust/function/README.md new file mode 100644 index 0000000..8d36a1d --- /dev/null +++ b/challenges/slugify/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# slugify/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/slugify/solutions/rust/function/src/main.rs b/challenges/slugify/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..30ff88d --- /dev/null +++ b/challenges/slugify/solutions/rust/function/src/main.rs @@ -0,0 +1,22 @@ +use std::io; + +fn main() { + let mut string = String::new(); + io::stdin() + .read_line(&mut string) + .expect("Failed to read `stdin` line."); + let string = string.trim().trim_matches('-'); + let mut answer = String::new(); + let mut current = String::new(); + for (_, character) in string.chars().enumerate() { + if character.is_whitespace() || (character == '-' && current.chars().count() > 0) { + answer.push_str(¤t); + answer.push('-'); + current.clear(); + } else if character.is_alphanumeric() { + current.push(character); + } + } + answer.push_str(¤t); + println!("{answer}"); +}