Dangling pointer
A dangling pointer occurs when a reference points to memory that has been de-allocated, leading to undefined behaviour. This can happen if a reference outlives the data it points to, posing a risk of accessing invalid or freed memory
fn main() {
let initial_text = String::from("Hi from main ");
let second_text = improve();
println!("{:?}", second_text);
}
fn improve()->&str{
let ss = String::from("Helloo");
&ss
}
error[E0106]: missing lifetime specifier
--> src/main.rs:8:15
|
8 | fn improve()->&str{
| ^ expected named lifetime
parameter
|
= help: this function's return type conta
ins a borrowed value, but there is no value
for it to be borrowed from
help: consider using the `'static` lifetime
|
8 | fn improve()->&'static str{
| +++++++
For more information about this error, try
`rustc --explain E0106`.
error: could not compile `dangling` (bin "d
angling") due to previous error
error[E0515]: cannot return reference to lo
cal variable `ss`
--> src/main.rs:10:5
|
10 | &ss
| ^^^ returns a reference to data ow
ned by the current function
fn main() {
let initial_text = String::from("Hi");
let second_text = improve();
println!("{:?}", second_text);
}
fn improve()->&'static str{
let ss ="Helloo";
ss
}
fn main() {
let initial_text = String::from("Hi from main ");
let second_text = improve();
println!("{:?}", second_text);
}
fn improve()->String{
let ss = String::from("Helloo");
ss
}