Questions & Results
What is the output?
let s = String::from("Rust is fun!");
let h = &s[5..7];
println!("{}", h);
What is the output?
let mut s1 = String::from("Go");
let s2 = " Terps";
s1.push_str(s2);
println!("[{}] [{}]", s1, s2);
What is the output?
let s1 = String::from("CMSC");
let s3;
{
let s2 = String::from("330");
s3 = s1 + &s2;
println!("{}", s2);
}
println!("{}", s3);
What is the output?
use std::collections::HashMap;
fn main() {
let mut h = HashMap::new();
h.insert("Ana", "1");
h.insert("Ben", "2");
h.insert("Ana", "3");
match h.get(&"Ana") {
Some(&id) => println!("Ana:{}", id),
_ => println!("Not Found"),
}
}
What is the output?
let v = vec![1, 2, 3, 4, 5];
println!("{:?}", v.get(100));
let oops = &v[100];
println!("{}", oops);
What is the output?
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let r = Rectangle { width: 30, height: 50 };
println!("{}", r);
}
True or false: point.y = 6; would compile at // HERE.
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Point { x: 0, y: 0 };
let mut point = point;
// HERE
}
What is the output?
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
impl Point {
fn m(&mut self) {
self.x += 1;
self.y += 2;
}
}
fn main() {
let mut p = Point { x: 0, y: 0 };
p.m();
p.m();
println!("{:?}", p);
}
What is the output?
#[derive(Debug)]
enum Auth {
Enabled(i32),
Disabled(i32),
}
fn main() {
let yes = Auth::Enabled(1);
let no = Auth::Disabled(0);
println!("{:?}", no);
}
What is the output?
enum Number {
Zero,
One,
Two,
}
fn main() {
use Number::Zero;
let t = Number::Two;
match t {
Zero => println!("0"),
Number::One => println!("1"),
_ => println!("other"),
}
}
Score (points) →