-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Examples hint the unhandy usage of static variables, leaving ownership to others #48
Comments
Hi, in Rust, |
Indeed. If the original owner goes out of scope, the cache points to an empty spot, which the compiler indeed detects. Your suggestion to use static indeed solves the problem as far as the compiler concerns, but effective points to strings in the memory of the executable: I expect that is not the intention of making this software cache? That's why I said in the title that the examples hint to use static variables. It would be nicer to demonstrate use of heap stored variables, but don't feel obliged if you think the example becomes too complicated. |
Yeah, you are right, it is not a good example. I am not aware of that because it was so long since I wrote the example. Thanks for pointing it out! |
I really appreciate it if you can help me modify it. |
* Cache takes ownership of input data The cache example now owns the data, and not static references, as mentioned in #48 Also variable names are modified to explain their usage. * Update sync_example.rs The cache example now owns the data, and not static references, as mentioned in #48 Also variable names are modified to explain their usage.
* Cache takes ownership of input data The cache example now owns the data, and not static references, as mentioned in #48 Also variable names are modified to explain their usage. * Update sync_example.rs The cache example now owns the data, and not static references, as mentioned in #48 Also variable names are modified to explain their usage.
When trying to modify both examples as a novice exercise, I stumbled onto the fact that static string slices are used. This immediately goes wrong when I simply move the creation of the cache to a separate function, e.g. in the async case:
The compiler will now rightfully complains:
Using references to string slices leaves the responsibility ownership to others: I would expect the cache takes up ownership, why create it otherwise? Do you accept a patch where I modify the structures to String types for both key and value?
The text was updated successfully, but these errors were encountered: