Answers for "rust•armanazi•reference•dangle"

0

rust•armanazi•reference•dangle

fn main() {
//    let reference_to_nothing = dangle();
let reference_to_nothing = no_dangle();
}
fn dangle() -> &String { // dangle returns a reference to a String

    let s = String::from("hello"); // s is a new String

    &s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away.
  // Danger!
//primitive types need to &'a or &'static 
  fn no_dangle() -> String {
    let s = String::from("hello");

    s
}

/*
no_dangle: This works without any problems. Ownership is moved out, and nothing is deallocated.
*/
Posted by: Guest on March-19-2022

Code answers related to "rust•armanazi•reference•dangle"

Browse Popular Code Answers by Language