The language, not the coloration of your two century old free trader.
I needed a mental break, so I poked at Rust a little. Here's code that generates a set of 6 numbers, from the computer roll of 2d6.
I needed a mental break, so I poked at Rust a little. Here's code that generates a set of 6 numbers, from the computer roll of 2d6.
Code:
// Really basic Rust. Get rust via 'rustup', then create a new project with
// cargo new chargen
//
// Then go into the chargen directory and edit src/main.rs as below.
//
// After that, run it to see if it works.
// cargo run
//
// Then build it.
// cargo build
// Imported via the Cargo.toml file
use rand::Rng;
// Returns an 8 bit unsigned integer.
// Like Ruby, the last successful command is the return value.
fn roll() -> u8 {
rand::thread_rng().gen_range(1..7)
}
// 2d6 for us Traveller people. Make your own 3d6 for DnD.
fn two_d6() -> u8 {
roll() + roll()
}
// The main function is where things happen.
fn main() {
// 'mut' means i is mutable, otherwise it would not be.
let mut i = 0;
while i < 6 {
i = i + 1;
// The ! means print is a macro. Still learning about those.
print!("{} ", two_d6());
}
// This is to add a newline after the results. If you're doing one line,
// you can get a free newline with println!.
// println!("My cool character.")
print!("\n")
}
// Here's a test function. The "#[test]" is the declaration.
// It is called with:
// cargo test
#[test]
fn test_roll() {
let mut counter = 0;
// a is a range, and the end of the range is not in the range.
let a = 1..7;
// Run this test 100 times.
while counter < 100 {
counter = counter + 1;
assert!(a.contains(&roll()));
}
}