Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created April 23, 2023 18:40
Show Gist options
  • Save 0ex-d/3681ff29467c86693e6d60754424db7f to your computer and use it in GitHub Desktop.
Save 0ex-d/3681ff29467c86693e6d60754424db7f to your computer and use it in GitHub Desktop.
Poking the Rust πŸ¦€ Borrow Checker.
// ❌ snippet_a
// This wouldn't work in Rust compiler because we're trying to change immutable data in memory
// https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#variables-and-mutability
// Look at the fix in *snippet_b*
let mut_data:Vec<(),()> = Vec::new();
for datum in set_of_data {
mut_data.push(datum);
}
// βœ… snippet_b
let mut mut_data:Vec<(),()> = Vec::new();
for datum in set_of_data {
mut_data.push(datum);
}
// snippet_c
// In *snippet_b* we had to make the variable mutable to modify the value in memory
// we can see how we manipulated the immutable data in "immut_data"
// do you think this trick would work? πŸ€”
let data_set = {
let immut_data:Vec<(),()> = Vec::new(); // πŸ¦€
for datum in set_of_data {
immut_data.push(datum);
}
immut_data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment