Rust would seem to have quite a few advantages for chess programming like speed, data safety and maybe simpler SMP with race condition safety. And other advantages. You can research that for yourselves.
The downside (or another upside) to Rust is that it is very strict and so the flexibility of C is lost. If it is an upside then it is because certain mistakes are caught at compile time rather than crash the program at run time.
I have written some sample code that looks like the beginning of a chess program. Well I hope it is anyway. It compiles without warning or errors and does what it is supposed to do. So this will be session one. We can talk about what should be done better. For example Rust seems to abhor global variables so if a function needs to modify global type variables then they must(?) be transferred by a mutable reference. So the first change to this code may be to put globals into a structure and just pass a mutable reference to the structure.
In the next session I will break what we already have up into files/modules known in Rust as crates. Therefore Cargo is necessary if you want to work with crates.
Here is the first sessions code:
Code: Select all
fn main() {
let command = 1u; // command is not mutable and is unsigned int
let analyze = 2u;
let compute = 3u;
let mut run = true; // run is mutable and is boolean
let mut computer = command;
let mut function = 0u;
let mut iteration = 0u;
initializef(&mut function, &mut computer); I like capitalized functions, not Rust
while run {
if computer == compute {computef(&mut function, &mut computer);}
if computer == analyze {analyzef(&mut function, &mut computer);}
if computer == command {commandf(&mut function, &mut computer, &mut run);}
iteration += 1;
println!("This is while iteration {}", iteration);
}
println!("End of program");
}
fn initializef(f: &mut uint, c: &mut uint) {
println!("function is Initialize {} computer = {}", *f, *c);
*f = 1u;
*c = 2u;
}
fn commandf(f: &mut uint, c: &mut uint, r: &mut bool) {
println!("function is Command {} computer = {}", *f, *c);
*r = false;
}
fn analyzef (f: &mut uint, c: &mut uint) {
println!("function is Analyze {} computer = {}", *f, *c);
*f = 2u;
*c = 3u;
}
fn computef (f: &mut uint, c: &mut uint) {
println!("function is Compute {} computer = {}", *f, *c);
*f = 3u;
*c = 1u;
}