Theory and Design of PL (CS 538)
March 09, 2020
Start early and ask for help!
rustc
rustc
by handcargo check
: Type/borrow checking (fast)cargo build
: Build an executable (slower)cargo run
: Run the thingcargo clean
: Clean up temporary filescargo clippy
)
cargo fmt
)
unsafe
code blocks.panic!
, unwrap
, expect
, …println!("value: {}", var)
io::stdin().read_line
read_line
returns something of type Result
Either
in Haskell: Ok(val)
or Err(e)
Cmp
trait gives comparison function (in Haskell, Ord
)// pattern match
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => { println!("Not a number! Picking 0..."); 0 },
}
// compare: cmp method coming from Cmp trait
match guess.cmp(&secret) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("Just right!"),
}
loop {
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
let guess_num: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
}
match guess_num.cmp(&secret) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => { println!("Just right!") ; break ; }
}
}
Copy
let x = 5; // OK
println!("The value of x is {}", x);
let y = x + 1; // OK
println!("The value of y is {}", y);
x = 6; // Not OK
println!("The value of x is {}", x);
let x = 5;
, can replace x by 5 belowlet x = 5;
println!("The value of x is {}", x); // 5
let x = x + 1;
println!("The value of x is {}", x); // 6
let x = x + 2;
println!("The value of x is {}", x); // 8
mut
for let-bindingsBoth may change the state!
let mut input = String::new();
io::stdin().read_line(&mut guess);
// ^--- Returns something!
println!("Guessed: {}" guess);
read_line
returns a value of type Result
let x = 42;
let y = 55;
let cmp_result = x.cmp(&y);
match cmp_result {
Ordering::Less => println!("so small!"),
Ordering::Equal => println!("exactly equal!"),
Ordering::Greater => println!("way big!"),
}
break
to exit loop, continue
to jump to start