First of all, the following code is correct:
fn main() {
let a = &get_i32();
println!("{}", a);
}
fn get_i32() -> i32 {
return 100;
}
But following code get error:
fn main() {
let a;
a = &get_i32();
println!("{}", a);
}
error[E0716]: temporary value dropped while borrowed
--> src/bin/rust_course.rs:8:10
|
8 | a = &get_i32();
| ^^^^^^^^^- temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
9 | println!("{}", a);
| - borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
8 ~ let binding = get_i32();
9 ~ a = &binding;
|
For more information about this error, try `rustc --explain E0716`.
What are the essential differences between these two pieces of code?I understand that &get_i32()
always returns a temporary value, so it should always report an error should all.
A similar problem:
fn main() {
let s1 = &String::from("hello world");
println!("{}", s1);
let s2 = String::from("hello world").as_str();
println!("{}", s2);
}
error[E0716]: temporary value dropped while borrowed
--> src/bin/rust_course.rs:6:14
|
6 | let s2 = String::from("hello world").as_str();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
7 | println!("{}", s2);
| -- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
6 ~ let binding = String::from("hello world");
7 ~ let s2 = binding.as_str();
|
For more information about this error, try `rustc --explain E0716`.
What's the difference between s1 and s2?
There is a more similar problem:
fn main() {
print(String::from("hello world").as_str());
}
fn print(str: &str) {
println!("{}", str);
}
The above code is correct, but I can't understand why String::from("hello world").as_str()
can be passed to functions but not assigned to variables.