Code

Ownership and Functions - Rust Programming

fn main() { let s1 = String::from("hello"); // Ownership of s1 is moved to the function takes_ownership(s1); let x = 5; // i32 is a Copy type, so x is still // valid after the function call makes_copy(x); } fn takes_ownership(some_string: String) { println!("{}", some_string); } // some_string goes out of scope // and the memory is freed fn makes_copy(some_integer: i32) { println!("{}", some_integer); } // some_integer goes out of scope, // but no resources need to be freed

0

1

Updated 2023-04-29

References


Tags

Object-Oriented Programming

Programming Language Paradigms

General programming languages

Computing Sciences