Created
April 23, 2023 18:40
-
-
Save 0ex-d/3681ff29467c86693e6d60754424db7f to your computer and use it in GitHub Desktop.
Poking the Rust π¦ Borrow Checker.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// β 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