Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Need help with lifetime
I am learning rust
I have a struct which has a borrowed field company_name
. At first the company_name
has borrowed value from old_company_name
so the lifetime is set till the old_company_name
lives. Then inside a new scope new_company_name
variable is defined and i have changed the value of company_name
filed to new_company_name
now the life time till which the emp
will live
is tied with new_company_name
, but if i can change the lifetime like this, then on reverting back to the company_name
to old_company_name
why do I get error ?
rohit@Rohits-MacBook-Air activities % cargo build -q --bin demo2
error[E0597]:new_company_name
does not live long enough
--> src/bin/demo2.rs:30:33
|
27 | let new_company_name = String::from("RiRo");
| ---------------- bindingnew_company_name
declared here
...
30 | emp.change_company_name(&new_company_name);
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
36 | }
| -new_company_name
dropped here while still borrowed
37 | println!("{:?}", old_company_name);
38 | println!("{:?}", emp);
| --- borrow later used here
For more information about this error, tryrustc --explain E0597
.
error: could not compileactivities
(bin "demo2") due to 1 previous error
#[derive(Debug)]
struct Employee<'a> {
name: String,
company_name: &'a str,
}
impl<'a> Employee<'a> {
fn change_name(&mut self, name: String) {
self.name = name
}
fn change_company_name(&mut self, new_name: &'a str) {
self.company_name = new_name;
}
}
fn main() {
let name = String::from("Rohit");
let old_company_name = String::from("RoRi");
let mut emp = Employee {
name: name,
company_name: &old_company_name,
};
println!("Working with Old Company ");
println!("{:?}", emp);
{
let new_company_name = String::from("RiRo");
emp.change_name(String::from("Riya"));
emp.change_company_name(&new_company_name);
println!("{:?}", emp);
emp.change_company_name(&old_company_name);
// new_company name scope end here
}
println!("{:?}", old_company_name);
println!("{:?}", emp);
}
I tried asking chatgpt and here the reponse
let name = String::from("Rohit Singh");
let old_company_name = String::from("RoRi");
let mut emp = Employee {
name: name,
company_name: &old_company_name,
};
Here:
emp
has lifetime 'a
referring to old_company_name
.{
let new_company_name = String::from("RiRo");
emp.change_name(String::from("Riya"));
emp.change_company_name(&new_company_name);
At this moment:
emp.company_name
points to new_company_name
.new_company_name
will be dropped at the end of this inner block.If you didnโt change it back before leaving the scope, emp
would end up holding a dangling reference after new_company_name
is dropped.
Thatโs why Rust would complain โ it enforces lifetime safety at compile time.
emp.change_company_name(&old_company_name);
This resets emp.company_name
to reference a variable that lives as long as emp
.
Now, when new_company_name
is dropped, emp
no longer depends on it, so thereโs no lifetime conflict.
3 posts - 2 participants
๐ท๏ธ Rust_feed