2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
36
Task/Caesar-cipher/Rust/caesar-cipher.rust
Normal file
36
Task/Caesar-cipher/Rust/caesar-cipher.rust
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
use std::io::{self, Write};
|
||||
use std::fmt::Display;
|
||||
use std::{env, process};
|
||||
|
||||
fn main() {
|
||||
let shift: u8 = env::args().nth(1)
|
||||
.unwrap_or_else(|| exit_err("No shift provided", 2))
|
||||
.parse()
|
||||
.unwrap_or_else(|e| exit_err(e, 3));
|
||||
|
||||
let plain = get_input()
|
||||
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)));
|
||||
|
||||
let cipher = plain.chars()
|
||||
.map(|c| {
|
||||
let case = if c.is_uppercase() {'A'} else {'a'} as u8;
|
||||
if c.is_alphabetic() { (((c as u8 - case + shift) % 26) + case) as char } else { c }
|
||||
}).collect::<String>();
|
||||
|
||||
println!("Cipher text: {}", cipher.trim());
|
||||
}
|
||||
|
||||
|
||||
fn get_input() -> io::Result<String> {
|
||||
print!("Plain text: ");
|
||||
try!(io::stdout().flush());
|
||||
|
||||
let mut buf = String::new();
|
||||
try!(io::stdin().read_line(&mut buf));
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn exit_err<T: Display>(msg: T, code: i32) -> ! {
|
||||
let _ = writeln!(&mut io::stderr(), "ERROR: {}", msg);
|
||||
process::exit(code);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue