Skip to content

Instantly share code, notes, and snippets.

@wunki
Created June 8, 2014 20:04
Show Gist options
  • Save wunki/dee82b7fdf588cb30e8d to your computer and use it in GitHub Desktop.
Save wunki/dee82b7fdf588cb30e8d to your computer and use it in GitHub Desktop.
Unique character check in Rust
use std::os;
fn unique_chars(s: &str) -> bool {
let v: Vec<char> = s.chars().collect();
let mut y = v.clone();
y.dedup();
v.len() == y.len()
}
fn main() -> () {
let args = os::args();
let sentence = args.get(1);
match unique_chars(sentence.as_slice()) {
true => println!("String contains only unique characters"),
false => println!("String doesn't contain only unique characters")
}
}
@Wowo10
Copy link

Wowo10 commented Dec 22, 2020

This will work only for sorted input.
According to official documentation dedup() deletes duplicates only consecutive repeated elements.

let mut vec = vec![1, 2, 2, 3, 2];

vec.dedup();

assert_eq!(vec, [1, 2, 3, 2]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment