Notepad3/test/test_files/StyleLexers/styleLexRust/Rust.rs

100 lines
4.9 KiB
Rust
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::rand::random;
use std::os;
use std::io::File;
fn main() {
let args: ~[~str] = os::args();
if args.len() != 2 {
println!("Usage: {:s} <inputfile>", args[0]);
} else {
let fname = args[1];
let path = Path::new(fname.clone());
let msg_file = File::open(&path);
match (msg_file) {
Some(mut msg) => {
let msg_bytes: ~[u8] = msg.read_to_end();
let share1_file
= File::create(&Path::new(fname + ".share1"));
let share2_file
= File::create(&Path::new(fname + ".share2"));
match (share1_file, share2_file) {
(Some(share1), Some(share2)) => {
split(msg_bytes, share1, share2);
} ,
(_, _) => fail!("Error opening output files!"),
}
} ,
None => fail!("Error opening message file: {:s}", fname)
}
}
}
split
abstract
fn xor(a: &[u8], b: &[u8]) -> ~[u8] {
let mut ret = ~[];
for i in range(0, a.len()) {
ret.push(a[i] ^ b[i]);
}
ret
}
fn split(msg_bytes: &[u8], mut share1: File, mut share2: File) {
let mut random_bytes: ~[u8] = ~[];
// This is not cryptographically strong randomness!
// (For entertainment purposes only.)
for _ in range(0, msg_bytes.len()) {
let random_byte = random();
random_bytes.push(random_byte);
}
let encrypted_bytes = xor(msg_bytes, random_bytes);
share1.write(random_bytes);
share2.write(encrypted_bytes);
}
fn main() {
if os::args().len() < 2 {
println("Error: Please provide a number as argument.");
return;
}
let i = from_str::<int>(os::args()[1]).unwrap();
println!("{:d} has {:d} Collatz steps", i, collatz(i));
let mut i = 0;
while i < 10 {
println("Hi there");
i += 1; // Rust doesn't support ++ or --
}
}
fn collatz(N: int) -> int {
if N == 1 { return 0; }
match N % 2 {
0 => { 1 + collatz(N/2) }
_ => { 1 + collatz(N*3+1) }
}
}
fn rprime_sum(x: int, y: int, m: int) {
match (x+y)%m {
0 => println("Multiple"),
_ => println("Relatively prime") // coment
}
}
let mut reference: &~int;
{
let val: ~int = ~10;
reference = &val;
} //val deallocated here
println!("{:d}", **reference); //Referencing something that's gone!