Does a mutable String reference implement Copy [duplicate] - rust

This question already has answers here:
Why is the mutable reference not moved here?
(4 answers)
Do mutable references have move semantics?
(1 answer)
Closed 1 year ago.
In the below code I was expecting the compiler to complain about use of moved value: xref on hello function's second call since mutable references do not implement Copy. The compiler doesn't raise any such error. What am I missing here?
fn main() {
let mut x: String = "Developer".to_string();
let x_ref: &mut String = &mut x;
hello(x_ref);
hello(x_ref);
}
fn hello(a: &mut String) {
println!("Hello {}", a);
}

Your example uses the mutable references after each other, which allows the compiler to perform an implicit reborrow. It basically turns this code into this:
hello(&mut *x_ref);
hello(&mut *x_ref);
Now you have two separate mutable borrows each only for the lifetime of the function call, thus they do not conflict with each other.
You see an error if you try to use it two times simultaneously.
fn main() {
let mut x: String = "Developer".to_string();
let x_ref: &mut String = &mut x;
hello(x_ref, x_ref);
}
fn hello(a: &mut String, b: &mut String) {
println!("Hello {} and {}", a, b);
}

Related

Rust - borrowed value does not live long enough [duplicate]

I want to write a program that will write a file in 2 steps.
It is likely that the file may not exist before the program is run. The filename is fixed.
The problem is that OpenOptions.new().write() can fail. In that case, I want to call a custom function trycreate(). The idea is to create the file instead of opening it and return a handle. Since the filename is fixed, trycreate() has no arguments and I cannot set a lifetime of the returned value.
How can I resolve this problem?
use std::io::Write;
use std::fs::OpenOptions;
use std::path::Path;
fn trycreate() -> &OpenOptions {
let f = OpenOptions::new().write(true).open("foo.txt");
let mut f = match f {
Ok(file) => file,
Err(_) => panic!("ERR"),
};
f
}
fn main() {
{
let f = OpenOptions::new().write(true).open(b"foo.txt");
let mut f = match f {
Ok(file) => file,
Err(_) => trycreate("foo.txt"),
};
let buf = b"test1\n";
let _ret = f.write(buf).unwrap();
}
println!("50%");
{
let f = OpenOptions::new().append(true).open("foo.txt");
let mut f = match f {
Ok(file) => file,
Err(_) => panic!("append"),
};
let buf = b"test2\n";
let _ret = f.write(buf).unwrap();
}
println!("Ok");
}
The question you asked
TL;DR: No, you cannot return a reference to a variable that is owned by a function. This applies if you created the variable or if you took ownership of the variable as a function argument.
Solutions
Instead of trying to return a reference, return an owned object. String instead of &str, Vec<T> instead of &[T], T instead of &T, etc.
If you took ownership of the variable via an argument, try taking a (mutable) reference instead and then returning a reference of the same lifetime.
In rare cases, you can use unsafe code to return the owned value and a reference to it. This has a number of delicate requirements you must uphold to ensure you don't cause undefined behavior or memory unsafety.
See also:
Proper way to return a new string in Rust
Return local String as a slice (&str)
Why can't I store a value and a reference to that value in the same struct?
Deeper answer
fjh is absolutely correct, but I want to comment a bit more deeply and touch on some of the other errors with your code.
Let's start with a smaller example of returning a reference and look at the errors:
fn try_create<'a>() -> &'a String {
&String::new()
}
Rust 2015
error[E0597]: borrowed value does not live long enough
--> src/lib.rs:2:6
|
2 | &String::new()
| ^^^^^^^^^^^^^ temporary value does not live long enough
3 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:15...
--> src/lib.rs:1:15
|
1 | fn try_create<'a>() -> &'a String {
| ^^
Rust 2018
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:2:5
|
2 | &String::new()
| ^-------------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
Is there any way to return a reference from a function without arguments?
Technically "yes", but for what you want, "no".
A reference points to an existing piece of memory. In a function with no arguments, the only things that could be referenced are global constants (which have the lifetime &'static) and local variables. I'll ignore globals for now.
In a language like C or C++, you could actually take a reference to a local variable and return it. However, as soon as the function returns, there's no guarantee that the memory that you are referencing continues to be what you thought it was. It might stay what you expect for a while, but eventually the memory will get reused for something else. As soon as your code looks at the memory and tries to interpret a username as the amount of money left in the user's bank account, problems will arise!
This is what Rust's lifetimes prevent - you aren't allowed to use a reference beyond how long the referred-to value is valid at its current memory location.
See also:
Is it possible to return either a borrowed or owned type in Rust?
Why can I return a reference to a local literal but not a variable?
Your actual problem
Look at the documentation for OpenOptions::open:
fn open<P: AsRef<Path>>(&self, path: P) -> Result<File>
It returns a Result<File>, so I don't know how you'd expect to return an OpenOptions or a reference to one. Your function would work if you rewrote it as:
fn trycreate() -> File {
OpenOptions::new()
.write(true)
.open("foo.txt")
.expect("Couldn't open")
}
This uses Result::expect to panic with a useful error message. Of course, panicking in the guts of your program isn't super useful, so it's recommended to propagate your errors back out:
fn trycreate() -> io::Result<File> {
OpenOptions::new().write(true).open("foo.txt")
}
Option and Result have lots of nice methods to deal with chained error logic. Here, you can use or_else:
let f = OpenOptions::new().write(true).open("foo.txt");
let mut f = f.or_else(|_| trycreate()).expect("failed at creating");
I'd also return the Result from main. All together, including fjh's suggestions:
use std::{
fs::OpenOptions,
io::{self, Write},
};
fn main() -> io::Result<()> {
let mut f = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("foo.txt")?;
f.write_all(b"test1\n")?;
f.write_all(b"test2\n")?;
Ok(())
}
Is there any way to return a reference from a function without arguments?
No (except references to static values, but those aren't helpful here).
However, you might want to look at OpenOptions::create. If you change your first line in main to
let f = OpenOptions::new().write(true).create(true).open(b"foo.txt");
the file will be created if it does not yet exist, which should solve your original problem.
You can not return a reference pointing to a local variable. You have two alternatives, either return the value or use a static variable.
Here is why:
References are pointers to memory locations. Once functions are executed, local variables are popped off the execution stack and resources are de-allocated. After that point, any reference to a local variable will be pointing to some useless data. Since it is de-allocated, it is not in our program's possession any more and OS may have already given it to another process and our data may have been overwritten.
For the following example, x is created when the function runs and dropped off when the function completes executing. It is local to the function and lives on this particular function's stack. Function's stack holds local variables.
When run is pop off the execution stack, any reference to x, &x, will be pointing to some garbage data. That is what people call a dangling pointer. The Rust compiler does not allow to use dangling pointers since it is not safe.
fn run() -> &u32 {
let x: u32 = 42;
return &x;
} // x is dropped here
fn main() {
let x = run();
}
So, that is why we can not return a reference to a local variable. We have two options: either return the value or use a static variable.
Returning the value is the best option here. By returning the value, you will be passing the result of your calculation to the caller, in Rust's terms x will be owned by the caller. In our case it is main. So, no problem there.
Since a static variable lives as long as the process runs, its references will be pointing to the same memory location both inside and outside the function. No problem there either.
Note: #navigaid advises using a box, but it does not make sense because you are moving readily available data to heap by boxing it and then returning it. It does not solve the problem, you are still returning the local variable to the caller but using a pointer when accessing it. It adds an unnecessary indirection due to de-referencing hence incurring additional cost. Basically you will be using & for the sake of using it, nothing more.
This is an elaboration on snnsnn's answer, which briefly explained the problem without being too specific.
Rust doesn't allow return a reference to a variable created in a function. Is there a workaround? Yes, simply put that variable in a Box then return it. Example:
fn run() -> Box<u32> {
let x: u32 = 42;
return Box::new(x);
}
fn main() {
println!("{}", run());
}
code in rust playground
As a rule of thumb, to avoid similar problems in Rust, return an owned object (Box, Vec, String, ...) instead of reference to a variable:
Box<T> instead of &T
Vec<T> instead of &[T]
String instead of &str
For other types, refer to The Periodic Table of Rust Types to figure out which owned object to use.
Of course, in this example you can simply return the value (T instead of &T or Box<T>)
fn run() -> u32 {
let x: u32 = 42;
return x;
}
Yes! But you have to find a way to extend the lifetime. One way to do that is to provide mutable reference to a dummy/default value (&mut T) to the function and then fill/replace the value in the function and then return a reference to that value (&T). This way you can specify the lifetime so that the returned reference get the lifetime of a value outside the function.
Examples:
//&mut T -> &T
fn example2<'a>(life: &'a mut Vec<i32>) -> &'a Vec<i32> {
*life = vec![1, 2, 3, 4];
life
}
fn test_example2() {
//Could also use Vec::new()
let mut life = Vec::default();
let res = example2(&mut life);
println!("{:?}", res)
}
fn test2_example2() {
let life = &mut Vec::default();
let res = example2(life);
println!("{:?}", res)
}
//shows real use case
fn get_check_test_slices2<'a>(
lifetime: &'a mut Vec<usize>,
limit: usize,
) -> impl Iterator<Item = (&'a [usize], &'a [usize])> + 'a {
// create a list of primes using a simple primes sieve
*lifetime = primes1_iter_bitvec(limit).collect::<Vec<_>>();
// iterate through pairs of sub slices without copying the primes vec
// slices will be used to check that a complicated sieve is correct
all_test_check_slices(lifetime)
}
Edit:
How is that different from just giving &mut T to the function? (asked by Chayim Friedman (see old solution below)):
It's basically the same... I lost against the borrow checker previously, and that is why I didn't just use &mut T. But after renewed battles I have finally managed to just use &mut T. Thank you for the insightful question.
Old solution:
My solution works by creating a default value before calling the function, which the function later replaces/fills and returns a reference to.
/// Used to return references to values created in a function.
/// fn example<'a>(lt:& 'a mut LifeExtender<Vec<i32>>) -> &'a Vec<i32> {
/// lt.set(vec![1,2,3,4]);
/// lt.get()
/// }
pub struct LifeExtender<T> {
value: T,
}
impl<T> Default for LifeExtender<T>
where
T: Default,
{
/// using T default.
fn default() -> Self {
Self {
value: T::default(),
}
}
}
impl<T> LifeExtender<T> {
/// If T doesn't have default.
pub fn new(value: T) -> Self {
Self { value }
}
/// set value to be returned by reference
pub fn set(&mut self, new_value: T) {
self.value = new_value;
}
/// Get a reference with lifetime self.
pub fn get<'a>(&'a self) -> &'a T {
&self.value
}
/// Get a mut reference with lifetime self.
pub fn get_mut<'a>(&'a mut self) -> &'a mut T {
&mut self.value
}
}
fn example<'a>(life: &'a mut LifeExtender<Vec<i32>>) -> &'a Vec<i32> {
let local_value = vec![1, 2, 3, 4];
life.set(local_value);
life.get()
}
//prints: [1,2,3,4]
pub fn test_example() {
let mut life = LifeExtender::default();
let res = example(&mut life);
println!("{:?}", res);
}
//Real example code snippet, where I used this solution:
fn get_check_slices2<'a>(
lifetime: &'a mut LifeExtender<Vec<usize>>,
limit: usize,
) -> impl Iterator<Item = (&'a [usize], &'a [usize])> + 'a {
lifetime.set(primes1_iter_bitvec(limit).collect::<Vec<_>>());
all_test_check_slices(lifetime.get())
}

Lifetimes when returning a reference to the contents of Rc<RefCell<_>> [duplicate]

This question already has answers here:
How do I return a reference to something inside a RefCell without breaking encapsulation?
(3 answers)
How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate]
(1 answer)
Closed 2 years ago.
How can I return a reference to something from inside a shared pointer (in this case Rc<RefCell<_>>)? In the example below, I show how it can be done with just a regular mutable reference to self, but if it becomes a shared pointer instead, the compiler gets angry that the return type has a missing lifetime specifier.
error[E0106]: missing lifetime specifier
--> src/main.rs:19:60
|
19 | fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &i32 {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
help: consider using the `'static` lifetime
|
19 | fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &'static i32 {
| ^^^^^^^^
use std::cell::RefCell;
use std::rc::Rc;
struct Thing {
items: Vec<i32>,
}
impl Thing {
fn new() -> Self {
Thing { items: Vec::new() }
}
fn add_to_self(&mut self, item: i32) -> &i32 {
self.items.push(item);
self.items.last().unwrap()
}
// does not compile
fn add_to_shared(me: Rc<RefCell<Thing>>, item: i32) -> &i32 {
me.borrow().items.push(item);
me.borrow().items.last().unwrap()
}
}
fn main() {
let mut thing = Thing::new();
println!("{}", thing.add_to_self(10));
let mut rc = Rc::new(RefCell::new(Thing::new()));
println!("{}", rc.add_to_shared(20));
}
Why do I want to do this? I have a program that builds a tree-like structure with multiple ownership. One of the associated methods takes two nodes of the tree (each shared pointers) and bundles them together into another part of the tree. Each method returns a reference to the newly created node so that it can be conveniently logged out (see the example).
I was thinking I'd need to use lifetime annotations to get this to work, but I have not been able to find out how do apply this concept to the interior of a Rc<RefCell<_>> type.
I think the problem here is that Rust knows how long self lives, but is not able to figure out how long Rc<RefCell<_>> exists. Do you need to return i32 references? If you would return just i32, the value would be copied and you would not have a reference into a struct that might not exists long enough.

Multiple mutable borrows when using result of a method call [duplicate]

This question already has answers here:
Why can't I store a value and a reference to that value in the same struct?
(4 answers)
Closed 2 years ago.
In this simplified code, I have two important structs: Owner takes ownership of an object, adds it to a Vec, and returns a reference to it; RefKeeper just holds references to objects in a Vec. Owner also has a RefKeeper.
struct Foo(i32);
struct Owner<'o> {
list: Vec<Foo>,
refkeeper: RefKeeper<'o>,
}
impl<'o> Owner<'o> {
pub fn new() -> Self {
Self {
list: Vec::new(),
refkeeper: RefKeeper::new(),
}
}
pub fn add(&mut self, me: Foo) -> &Foo {
self.list.push(me);
return self.list.last().unwrap();
}
pub fn add_ref(&mut self, me: &'o Foo) {
self.refkeeper.add(me);
}
}
struct RefKeeper<'ro> {
list: Vec<&'ro Foo>,
}
impl<'ro> RefKeeper<'ro> {
pub fn new() -> Self {
Self { list: Vec::new() }
}
pub fn add(&mut self, me: &'ro Foo) {
self.list.push(me);
}
}
fn main() {
let mut owner = Owner::new();
let a1 = Foo(1);
let a1_ref = owner.add(a1);
// this variant doesn't work
owner.add_ref(a1_ref);
// let mut refkeeper = RefKeeper::new();
// refkeeper.add(a1_ref);
// let a2 = Foo(2);
// owner.add_ref(&a2);
}
Two variants work: if I make RefKeeper externally, I can store the ref returned by Owner::add; on the other hand, if I make a new object (a2), I can store &a2 in owner.refkeeper with no problem. Why does the other variant give me this error?
error[E0499]: cannot borrow `owner` as mutable more than once at a time
--> src/main.rs:46:5
|
43 | let a1_ref = owner.add(a1);
| ----- first mutable borrow occurs here
...
46 | owner.add_ref(a1_ref);
| ^^^^^ ------ first borrow later used here
| |
| second mutable borrow occurs here
Is there something fundamentally wrong with this pattern? I'm under the impression that there should be no issue with lifetimes because all the borrows are being used in the same object.
Its because Owner::add returns a reference that is bound to the lifetime of &mut self. So as long as the return value exists (a1_ref) then the &mut self reference does too. So calling add_ref fails because it requires another mut self reference of that same instance.
You can either have one mutable reference, or many immutable references.
The reason calling refkeeper.add and then owner.add_ref is not giving you the same issue is because adding to refkeeper does not require another reference to owner.
You might want to look at std::rc::Rc.

rust: expected lifetime parameter problem [duplicate]

This question already has answers here:
How to return new data from a function as a reference without borrow checker issues?
(1 answer)
Return local String as a slice (&str)
(7 answers)
Closed 3 years ago.
I am new to Rust and was making a simple program to practice which takes 2 numbers and prints the numbers between them. This is my code
fn main() {
let a: u32 = 2;
let b: u32 = 10;
let a = result(&a, &b);
println!("{:?}", a);
}
fn result(a: &u32, b: &u32) -> [Vec<&u32>] {
let mut vec = Vec::new();
for x in a..b {
vec.push(a);
}
vec
}
When i run this program, i get this error
fn result(a: &u32, b: &u32) -> [Vec<&u32>] {
| ^ expected lifetime parameter
The concepts of borrowing and lifetimes are still very new and strange to me. Please help me in determining where i am wrong.
The error message is quite clear here:
this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from a or b
Providing an explicit lifetime would help the compiler understand what you're trying to do:
fn result<'a>(a: &'a u32, b: &'a u32) -> Vec<&'a u32> {
let mut vec: Vec<&'a u32> = Vec::new();
for x in *a..*b {
vec.push(&x);
}
vec
}
But the above wouldn't work either, as borrowing x results in borrowing a variable whch only lives in the scope a single iteration:
vec.push(&x);
^^ borrowed value does not live long enough
What you probably want is to avoid borrowing altogether:
fn result(a: u32, b: u32) -> Vec<u32> {
let mut vec = Vec::new();
for x in a..b {
vec.push(x);
}
vec
}
live example on godbolt.org

Cannot build equivalent closure for a function when the closure contains complex lifetime relationships [duplicate]

This question already has answers here:
How to declare a higher-ranked lifetime for a closure argument?
(3 answers)
Closed 3 years ago.
I've got some trouble with replacing function with equivalent closure, compiler complaining
cannot infer an appropriate lifetime due to conflicting requirements
note: ...so that the types are compatible:
expected &std::collections::BTreeSet<&str>
found &std::collections::BTreeSet<&str> rustc(E0495)
in the closure, at the rangemethod in r.extend(s.range(lower..=upper));.
But i can't figure out how to put lifetime hint in a closure, and maybe, it's not possible.
use std::collections::BTreeSet;
fn main() {
let mut set = BTreeSet::new();
set.insert("TEST1");
set.insert("TEST3");
set.insert("TEST4");
set.insert("TEST2");
set.insert("TEST5");
println!("init: {:?}", set);
let closure = |lower, upper| {
|s: &BTreeSet<&str>| {
let mut r = BTreeSet::new();
r.extend(s.range(lower..=upper));
r
}
};
set = extract_fn("TEST2", "TEST5")(&set);
set = closure("TEST3", "TEST4")(&set);
println!("result: {:?}", set);
}
fn extract_fn<'a>(
lower: &'a str,
upper: &'a str,
) -> impl Fn(&BTreeSet<&'a str>) -> BTreeSet<&'a str> {
move |s| {
let mut r = BTreeSet::new();
r.extend(s.range(lower..=upper));
r
}
}
Except putting static lifetime, should a closure with this type of error be transformed to a function ?
This cannot be done easily but you can define your outer closure with a return type which helps you to set explicit lifetime boundaries for your inner closure. (By using for<> which is a Higher Ranked Trait Bound, you can find more details in here).
Inner closure needed to be Boxed because the size of Fn trait is not known at compile time.
let closure = |lower, upper| -> Box<for<'a> Fn(&BTreeSet<&'a str>) -> BTreeSet<&'a str>> {
Box::new(move |s: &BTreeSet<&str>| {
let mut r = BTreeSet::new();
r.extend(s.range(lower..=upper));
r
})
};
Playground

Resources