Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•reference•dangle

{
The main aim of lifetimes is to prevent dangling references.
which has an outer scope and an inner scope.
}

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.
*/
Comment

PREVIOUS NEXT
Code Example
Rust :: armanriazi•rust•mem•leak 
Rust :: armanriazi•rust•mem•deallocating 
Rust :: armanriazi•rust•error•E0220•associated type `` not found for `Self` 
Rust :: rust absolute path 
Rust :: overwritting print on same line rust 
Rust :: rust Clean way to get Option::unwrap_or_else behaviour with an Option<&T 
Rust :: armanriazi•rust•error•E0605•non-primitive cast 
Rust :: how to get text from a file and store it in a variable rust 
Rust :: how to get public addres from private key solana cli 
Rust :: armanriazi•rust•trait•object•safe 
Rust :: primitive data types in rust 
Rust :: rust match enum 
Lua :: roblox rainbow part 
Lua :: roblox lua on player chatted 
Lua :: roblox index and newindex 
Lua :: roblox tween part color 
Lua :: luau table.find() 
Lua :: lua gsub 
Lua :: prompt developer product purchase roblox 
Lua :: lua table unpack 
Lua :: lua convert function to string 
Lua :: roblox touch part kill script 
Lua :: attempt to call a string value lua 
Lua :: difference between roblox and minecraft 
Lua :: How to create a part with script in roblox srudio 
Matlab :: streamline matlab 
Matlab :: : in matlab 
Basic :: basic 
Basic :: visual basic how to dynamically change a button to bold 
Elixir :: liveview component mount 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =