Skip to content

Instantly share code, notes, and snippets.

@stellasia
Last active February 24, 2022 14:19
Show Gist options
  • Select an option

  • Save stellasia/601316e54ead6507008e302f4e96af6a to your computer and use it in GitHub Desktop.

Select an option

Save stellasia/601316e54ead6507008e302f4e96af6a to your computer and use it in GitHub Desktop.
Medium_From_Python_To_Rust_Transaction
class Transaction:
def execute_query(self, query: str):
# run query
pass
def close(self):
# close transaction
pass
def do_stuff(tx: Transaction):
# do stuff
# commit or rollback
tx.close()
def main():
tx = Transaction()
do_stuff(tx)
# will raise error at Runtime, but nothing prevents you from writting the following line:
tx.close()
struct Transaction {}
impl Transaction {
pub fn execute_query(&self, query: &str) {
// function "borrows" self => transaction is rendered to main thread
// run query
}
pub fn close(self) {
// function "takes ownership" of self, which does not exist anymore in main thread!
}
}
fn do_stuff(tx: Transaction) {
// do stuff
// function takes ownership of "tx"
tx.close();
}
fn main() {
tx = Transaction {} ;
do_stuff(tx);
// tx is not useable anymore here
// tx.close() // => will raise error at compilation!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment