Reputation: 5111
So in the below function I didn't use &content
while passing the parameter to func
. How is content
available after func(content)
?
How do you move the content
reference to func
so that the reference dies inside the func
and will not be available afterwards the call if that even makes sense? I want to just free the memory afterwards.
fn main() {
let content = "Hello World";
func(content);
println!("{}", content);
}
fn func(string: &str) {
println!("{}", &string);
}
Upvotes: 0
Views: 469
Reputation: 35512
You seem to have a couple misunderstandings in the comments. The fact that you can call func(content)
and still use content
has nothing to do with the fact that content
has a static lifetime. It has to do with the fact that immutable references implement Copy
, so they can't be "moved." It's like how this code works despite not having any static references:
fn func<T>(_: T) {}
fn main() {
let x = 5;
let y = &x;
func(y);
println!("{}", y);
}
Even if you have something large that you want to eventually free, you don't need to do anything with the reference. Once the scope is exited (e.g. the function returns), Rust will drop all local variables, freeing any memory if necessary.
Upvotes: 2