Oddbean new post about | logout
 #Rust is one of the only languages where you can tie the lifetime of an object to another one. This ‘a thing is a lifetime parameter that says “Note must not outlive  Transaction”. Which is incredibly useful since in #nostrdb the pointer to the memory-mapped data is only valid as long as the transaction exists. This is enforced at compile time, making data-read-outside-of-transaction crashes impossible.

gm #nerdstr

https://cdn.jb55.com/s/31256b0e5dc9bd93.png 
 GM🌞 #Damus 
 Rust is such a great programming language 🦀 
 You can accomplish the same using smart pointers in c++ although rust's way is less error prone.
I don't get what you mean with "enforced at compile time". The lifetime of an object can't be known at compile time (unless the object is static obviously).
Do you mean more like "it's enforced that the compiler's codegen will enforce the lifetimes are coupled during runtime"? 
 no its enforced at compile time. Lifetimes are typechecked by the compiler. If you try to keep a note alive longer than a transaction it won’t compile. 
 How can the lifetime be checked at compile time? Magic?
The only thing I can think of is working with worst case scenario lifetimes by using the declaration scopes. 
 Its not that hard:

{
  let txn = Transaction::new()
}

In this case the compiler knows when it will be deconstructed. If note holds a reference to this beyond this scope then it is easily caught by the compiler. The ‘a parameter links the two references and can catch when one outlives the other. 
 I'm thinking that you can have code that conditionally at runtime might destroy an object and free its memory.
Probably rustc just assumes the worst 
 I don’t think you can explicitly do this in rust except in an unsafe block 
 When this releases is there some way that I can learn its ins and outs?I've missed web development and database tech in my education and would love to pick it up for nostr development 
 If you want to learn programming I'd avoid starting with rust. Also, even if possible, building webservers with rust might not be not only the most obvious choice imho, but neither the best to work with for this goal 
 Note that I have nothing against Rust., I love Rust and have been using it on most of my side projets in the last 2 years.
 
 makes me want to learn Rust 
 I concur