Last active
December 26, 2015 03:09
-
-
Save joladev/7084285 to your computer and use it in GitHub Desktop.
Attempt at random string generation in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::rand; | |
use std::rand::Rng; | |
use std::str; | |
static TARGET: &'static str = "methinks it is like a weasel"; | |
static ALPHABET: &'static [u8] = bytes!("abcdefghijklmnopqrstuvwxyz"); | |
fn random_char() -> u8 { | |
rand::task_rng().choose(ALPHABET) | |
} | |
fn random_string () -> ~str { | |
let mut result = str::with_capacity(TARGET.len()); | |
for _ in range(0, TARGET.len()) { | |
result.push_char(random_char() as char); | |
} | |
result | |
} | |
fn main() { | |
println(random_string()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something I came up with in Rust 0.11