Why can borrowed string literal outlive its owner by faking a lifetime? - rust

I understand that a borrow cannot outlive the existence of the thing it points to, to eradicate the dangling pointers.
A borrow or an alias can outlive the owner by faking the lifetimes:
fn main() {
let e;
let first = "abcd";
{
let second = "defgh";
e = longest(first, second);
}
println!("{}", e);
}
fn longest<'a>(first: &'a str, second: &'a str) -> &'a str {
if first.len() > second.len() {
first
} else {
second
}
}
Result:
defgh
In the above example, the variable e has a longer lifetime than the second variable and clearly the first & second variables lifetimes are different.
When e is initialized with longest(first, second) it gets the second variable whose lifetime to function call is faked as it is equal to first but it is confined to the block and it is assigned to e which will outlive the second. Why is this OK?

This is due to the fact that both of these have the 'static lifetime.
Here's an example that doesn't work because the str here does not live for the life of the program like a &'static str does.
The only change is the following line: let second = String::from("defgh"); and the next line where it is passed to the longest function.
fn main() {
let e;
let first = "abcd";
{
let second = String::from("defgh");
e = longest(first, &second);
}
println!("{}", e);
}
fn longest<'a>(first: &'a str, second: &'a str) -> &'a str {
if first.len() > second.len() {
first
} else {
second
}
}
Here's the error:
error[E0597]: `second` does not live long enough
--> src/main.rs:6:28
|
6 | e = longest(first, &second);
| ^^^^^^^ borrowed value does not live long enough
7 | }
| - `second` dropped here while still borrowed
8 | println!("{}", e);
| - borrow later used here
More information can be found in Static - Rust By Example

Related

How do lifetimes in Rust work for a function?

In the definition of lifetime_things, the lifetime of 'b is longer than 'a, but actually when I call this function, x1 is longer than y1, but this can compile successfully:
//here you could see 'b:'a means, the lifetime of b should be larger than a,
fn lifetime_things<'a,'b:'a>(x:&'a String, y:&'b String) ->&'a String {
if x.len() > y.len() {
&x
} else {
&y
}
}
fn main() {
let z: i32;
let x = String::from("1");
let x1=&x;
{
let y = String::from("2");
let y1=&y;
println!("{}",lifetime_things(x1,y1));
}
}
But here you could see the lifetime of x1 should be larger than y1 so why can this compile successfully as well?
'b: 'a means 'b must outlive 'a, which in turn means whenever 'a is live, so must 'b. Crucially, this is a larger than or equals relationship. Also notice that lifetimes don't work like types. When a function takes an 'v reference, any lifetime ('w) will be accepted as long as 'w: 'v.
So, where is 'a live? In the body of the function and whenever the returned string reference is used after the call. We can visualize it like this:
fn lifetime_things<'a,'b:'a>(x:&'a String, y:&'b String) ->&'a String {
if x.len() > y.len() { // <-+
&x // |
} else { // |
&y // +----+
} // | |
} // <-+ |
// |
fn main() { // +-- 'a
let z: i32; // |
let x = String::from("1"); // |
let x1=&x; // |
{ // |
let y = String::from("2"); // |
let y1=&y; // |
println!("{}",lifetime_things(x1,y1)); // <------+
}
}
Notice, that since the value returned by lifetime_things is only printed, 'a is only live on the line println!("{}",lifetime_things(x1,y1)); (exclusing lifetime_thingss body).
So, to call lifetime_things we simply need the both arguments to at least be live at the call. This holds for both x1 and y1, so it all checks out.

Iterate through a whole file one character at a time

I'm new to Rust and I'm struggle with the concept of lifetimes. I want to make a struct that iterates through a file a character at a time, but I'm running into issues where I need lifetimes. I've tried to add them where I thought they should be but the compiler isn't happy. Here's my code:
struct Advancer<'a> {
line_iter: Lines<BufReader<File>>,
char_iter: Chars<'a>,
current: Option<char>,
peek: Option<char>,
}
impl<'a> Advancer<'a> {
pub fn new(file: BufReader<File>) -> Result<Self, Error> {
let mut line_iter = file.lines();
if let Some(Ok(line)) = line_iter.next() {
let char_iter = line.chars();
let mut advancer = Advancer {
line_iter,
char_iter,
current: None,
peek: None,
};
// Prime the pump. Populate peek so the next call to advance returns the first char
let _ = advancer.next();
Ok(advancer)
} else {
Err(anyhow!("Failed reading an empty file."))
}
}
pub fn next(&mut self) -> Option<char> {
self.current = self.peek;
if let Some(char) = self.char_iter.next() {
self.peek = Some(char);
} else {
if let Some(Ok(line)) = self.line_iter.next() {
self.char_iter = line.chars();
self.peek = Some('\n');
} else {
self.peek = None;
}
}
self.current
}
pub fn current(&self) -> Option<char> {
self.current
}
pub fn peek(&self) -> Option<char> {
self.peek
}
}
fn main() -> Result<(), Error> {
let file = File::open("input_file.txt")?;
let file_buf = BufReader::new(file);
let mut advancer = Advancer::new(file_buf)?;
while let Some(char) = advancer.next() {
print!("{}", char);
}
Ok(())
}
And here's what the compiler is telling me:
error[E0515]: cannot return value referencing local variable `line`
--> src/main.rs:37:13
|
25 | let char_iter = line.chars();
| ---- `line` is borrowed here
...
37 | Ok(advancer)
| ^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0597]: `line` does not live long enough
--> src/main.rs:49:34
|
21 | impl<'a> Advancer<'a> {
| -- lifetime `'a` defined here
...
49 | self.char_iter = line.chars();
| -----------------^^^^--------
| | |
| | borrowed value does not live long enough
| assignment requires that `line` is borrowed for `'a`
50 | self.peek = Some('\n');
51 | } else {
| - `line` dropped here while still borrowed
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0515, E0597.
For more information about an error, try `rustc --explain E0515`.
error: could not compile `advancer`.
Some notes:
The Chars iterator borrows from the String it was created from. So you can't drop the String while the iterator is alive. But that's what happens in your new() method, the line variable owning the String disappears while the iterator referencing it is stored in the struct.
You could also try storing the current line in the struct, then it would live long enough, but that's not an option – a struct cannot hold a reference to itself.
Can you make a char iterator on a String that doesn't store a reference into the String? Yes, probably, for instance by storing the current position in the string as an integer – it shouldn't be the index of the char, because chars can be more than one byte long, so you'd need to deal with the underlying bytes yourself (using e.g. is_char_boundary() to take the next bunch of bytes starting from your current index that form a char).
Is there an easier way? Yes, if performance is not of highest importance, one solution is to make use of Vec's IntoIterator instance (which uses unsafe magic to create an object that hands out parts of itself) :
let char_iter = file_buf.lines().flat_map(|line_res| {
let line = line_res.unwrap_or(String::new());
line.chars().collect::<Vec<_>>()
});
Note that just returning line.chars() would have the same problem as the first point.
You might think that String should have a similar IntoIterator instance, and I wouldn't disagree.

Lifetime issues in rust: borrowed value does not live long enough

I have reproduced my problem in the short code below.
Problem: The inner thread uses reference of variable v from the outer thread. The rust compiler throws an error because "technically" the outer thread could terminate before the inner thread and hence inner thread could loose access to variable v. However in the code below that clearly cannot happen.
Question: How shall I change this code so that it complies while maintaining the same functionality?
fn main() { //outer thread
let v = vec![0, 1];
let test = Test { v: &v }; //inner_thread
std::thread::spawn(move || test.print());
loop {
// this thread will never die because it will never leave this loop
}
}
pub struct Test<'a> {
v: &'a Vec<u32>,
}
impl<'a> Test<'a> {
fn print(&self) {
println!("{:?}", self.v);
}
}
error[E0597]: `v` does not live long enough
--> src/main.rs:3:26
|
3 | let test = Test { v: &v }; //inner_thread
| ^^ borrowed value does not live long enough
4 | std::thread::spawn(move || test.print());
| ---------------------------------------- argument requires that `v` is borrowed for `'static`
...
8 | }
| - `v` dropped here while still borrowed
The obvious solution would be to have Test own the vector instead of just have a reference.
But if you really need to borrow the value in the thread (probably because you want to use it after end of execution), then you may use crossbeam's scope:
let v = vec![0, 1];
let test = Test { v: &v }; //inner_thread
crossbeam::thread::scope(|scope| {
scope.spawn(|_| test.print());
}).unwrap();

How does ownership and borrowing work when making slices?

Here are two code snippets, but they show a different behavior and I couldn't understand what's going on in there. Their main function is identical. If the borrowing and ownership concept applies to one code (i.e to code 2) why not to another code i.e (code 1)?
code 1:
This code compiles with no errors and prompt the result.
fn main() {
let mut s = String::from("Hello world");
let result = first_word(&s);
s.clear();
println!("result:{:#?}", result);
}
fn first_word(s: &String) -> usize {
let s = s.as_bytes();
//println!("{:?}",s);
for (i, &item) in s.iter().enumerate() {
if item == 32 {
return i;
}
}
s.len()
}
Code 1 Output :
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running `target/debug/rust_Slices`
result:5
Code 2:
This code won't compile and gives an error.
fn main() {
let mut s = String::from("Hello world");
let result = first_word(&s);
s.clear();
println!("{:#?}", result);
}
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
Code 2 Output:
cannot borrow `s` as mutable because it is also borrowed as immutable
--> src/main.rs:4:4
|
3 | let result = first_word(&s);
| -- immutable borrow occurs here
4 | s.clear();
| ^^^^^^^^^ mutable borrow occurs here
5 | println!("{:#?}",result);
| ------ immutable borrow later used here
Let's decompose:
// Let's build a string, which basically is a growable array of chars
let mut s = String::from("Hello world");
// now make result a slice over that string, that is a reference
// to a part of the underlying chars
let result = first_word(&s);
// now let's remove the content of the string (which of course removes
// what the slice was referring to)
s.clear();
// and let's... print it ?
println!("{:#?}", result);
Hopefully the borrow checker prevents you from doing this with this exact error:
cannot borrow s as mutable because it is also borrowed as immutable
And if you've understood this, the solution should be obvious: don't make result a window over another string but a string by itself, having its own content: change the second line to
let result = first_word(&s).to_string();
Now you can clear the source string and keep the first word. Of course to_string() isn't a costless operation so you might want to try keep the source string around in real applications.
The key thing here is lifetimes. By default lifetimes argument for functions with one input reference and output reference are the same (liftime elision). So compiler implicitly changes the code following way:
fn first_word<'a>(s: &'a String) -> &'a str { // note 'a here
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
That means that the result borrows the input argument. You can explicitly make lifetimes different and eliminate error in the main but in this case first_word will not compile:
fn first_word1<'a, 'b>(s: &'a String) -> &'b str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:7:21
|
7 | return &s[0..i];
| ^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body

Mutably borrowing in match statement and result

I'm trying to determine if a container has an object and return the found object if it does, or add it if it doesn't.
I've found Rust borrow mutable self inside match expression
which has an answer which says what I am trying to do can't (couldn't?) be done.
In my situation, I've got some objects that have vectors of children. I don't want to expose the internals of my object, because I may want to change the representation underneath.
How can you resolve the need to mutably borrow in different match arms in Rust? seems to suggest I may be able to do what I want if I get the lifetimes correct, but I haven't been able to figure out how.
Here's a representation of the issue I'm having:
fn find_val<'a>(container: &'a mut Vec<i32>, to_find: i32) -> Option<&'a mut i32> {
for item in container.iter_mut() {
if *item == to_find {
return Some(item);
}
}
None
}
fn main() {
let mut container = Vec::<i32>::new();
container.push(1);
container.push(2);
container.push(3);
let to_find = 4;
match find_val(&mut container, to_find) {
Some(x) => {
println!("Found {}", x);
}
_ => {
container.push(to_find);
println!("Added {}", to_find);
}
}
}
playground
The error I get is:
error[E0499]: cannot borrow `container` as mutable more than once at a time
--> src/main.rs:24:13
|
19 | match find_val(&mut container, to_find) {
| --------- first mutable borrow occurs here
...
24 | container.push(to_find);
| ^^^^^^^^^ second mutable borrow occurs here
...
27 | }
| - first borrow ends here
Put the change in a function, and use early return instead of an else branch:
fn find_val_or_insert(container: &mut Vec<i32>, to_find: i32) {
if let Some(x) = find_val(&container, to_find) {
println!("Found {}", x);
return; // <- return here instead of an else branch
}
container.push(to_find);
println!("Added {}", to_find);
}
See also Mutable borrow more than once and How to update-or-insert on a Vec?

Resources