JavaScript
const test = [0]
test.push('hi')
test.push(2)
test.forEach(i => console.log(`Current item is: ${i}`))
Ruby
test = [0]
test.push('hi')
test.push(2)
test.each { |i| puts "Current item is: #{i}" }
Rust
fn main() {
let mut test: [i32; 3] = [0; 3];
test[1] = 1;
test[2] = 2;
for x in &test {
println!("Current number is: {} ", x);
}
}
Rust: Ownership
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1); // error!
}
Rust: the compiler
error[E0382]: use of moved value: `s1`
--> src/main.rs:5:28
|
3 | let s2 = s1;
| -- value moved here
4 |
5 | println!("{}, world!", s1);
| ^^ value used here after move
|
= "note": move occurs because `s1` has type `std::string::String`, which does
not implement the `Copy` trait
Talk sponsored by
loop {
let mut action = String::new();
io::stdin()
.read_line(&mut action)
.ok()
.expect("Failed to read line");
match action.trim() {
"scavenge" | "s" => scavenge(&mut inventory, &mut stats),
// ...
_ => println!("Invalid input"),
}
}
type Inventory = Vec<Item>
let mut inventory: Inventory = Vec::new();
// ...
inventory.push(item);
struct Item {
name: &'static str,
description: &'static str,
value: ItemStats,
risk: f64,
uses_until_breakdown: i32,
consumable: bool,
}
Item {
name: "Wood",
description: "Useful for crafting",
value: Stats {
health: 0.0,
food: 0.0,
water: 0.0,
energy: 0.0,
},
uses_until_breakdown: 0,
consumable: false,
risk: 0.0,
},
enum ItemProperties {
StandardItem,
ConsumeableItem {
value: ItemStats,
risk: f64,
},
ToolItem {
uses_until_breakdown: i32,
},
}
struct Item {
name: &'static str,
description: &'static str,
properties: ItemProperties,
}
impl Item {
pub fn decrease_use(&mut self) -> bool {
// ...
}
}
Item {
name: "Wood",
description: "Useful for crafting",
properties: ItemProperties::StandardItem,
}
{
startStatsTimer () {
this.loop = setTimeout(() => {
if (this.isActive) {
this.decreaseStats()
this.startStatsTimer()
}
}, 12 * 1000)
}
}
let now = Instant::now();
let mut elapsed_time = now.elapsed().as_secs();
loop {
let current_elapsed_time = now.elapsed().as_secs();
let seconds = current_elapsed_time - elapsed_time;
elapsed_time = current_elapsed_time;
decrease_stats(&mut stats, seconds as f64);
// Ask user for input ...
}
thread::spawn(move || loop {
thread::sleep(Duration::from_secs(10));
println!("Now we should decrease stats…");
});
fn main() {
let stats = Arc::new(Mutex::new(Stats {
water: Stat::new(100.0),
food: Stat::new(100.0),
energy: Stat::new(100.0),
}));
control_time(&stats);
// ...
}
fn control_time(stats: &Arc<Mutex<Stats>>) {
let stats = Arc::clone(&stats);
thread::spawn(move || loop {
thread::sleep(Duration::from_secs(10));
let mut stats_lock = stats.lock().unwrap();
decrease_stats(&mut stats_lock, 10.0);
});
}
let (tx, rx) = mpsc::channel();
let (tx2, rx2) = mpsc::channel();
JavaScript
document.dispatchEvent(new CustomEvent('hi!'))
Rust
tx.send("hi");
// ...
// rx.recv();
thread::spawn(move || loop {
let _ = rx2.recv();
let action = request_input("\nWhat to do?");
tx.send(action).ok();
});
loop {
if let Ok(action) = rx.try_recv() {
match action.trim() {
// handle all possible actions
}
}
if is_game_over(&stats.lock().unwrap()) {
break;
} else {
tx2.send(String::from("Ready for input")).ok();
}
}
loop {
if let Ok(action) = rx.try_recv() {
match action.trim() {
// handle all possible actions
}
// now we are ready for another action:
tx2.send(String::from("Ready for input")).ok();
}
if is_game_over(&stats.lock().unwrap()) {
break;
}
}
Overwhelmed?
If I can do it, so can you.