mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add slugify/rust/function
This commit is contained in:
parent
1d87185e58
commit
257ab19f53
7
challenges/slugify/solutions/rust/function/Cargo.lock
generated
Normal file
7
challenges/slugify/solutions/rust/function/Cargo.lock
generated
Normal file
@ -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"
|
6
challenges/slugify/solutions/rust/function/Cargo.toml
Normal file
6
challenges/slugify/solutions/rust/function/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
3
challenges/slugify/solutions/rust/function/README.md
Normal file
3
challenges/slugify/solutions/rust/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# slugify/rust/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.
|
22
challenges/slugify/solutions/rust/function/src/main.rs
Normal file
22
challenges/slugify/solutions/rust/function/src/main.rs
Normal file
@ -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}");
|
||||
}
|
Loading…
Reference in New Issue
Block a user