Logo

Programming-Idioms

History of Idiom 209 > diff from v9 to v10

Edit summary for version 10 by programming-idioms.org:
[Rust] Variable name v (idiom statement was updated to clarify)

Version 9

2020-03-20, 17:26:24

Version 10

2020-03-20, 17:27:34

Idiom #209 Type with automatic deep deallocation

Declare a type t which contains a string s and an integer array n with variable size, and allocate a variable v of type t. Allocate v.s and v.n and set them to the values "Hello, world!" for s and [1,4,9,16,25], respectively. Deallocate v, automatically deallocating v.s and v.n (no memory leaks).

Idiom #209 Type with automatic deep deallocation

Declare a type t which contains a string s and an integer array n with variable size, and allocate a variable v of type t. Allocate v.s and v.n and set them to the values "Hello, world!" for s and [1,4,9,16,25], respectively. Deallocate v, automatically deallocating v.s and v.n (no memory leaks).

Code
struct T {
	s: String,
	n: Vec<usize>,
}

fn main() {
	let a = T {
		s: "Hello, world!".into(),
		n: vec![1,4,9,16,25]
	};
}
Code
struct T {
	s: String,
	n: Vec<usize>,
}

fn main() {
	let v = T {
		s: "Hello, world!".into(),
		n: vec![1,4,9,16,25]
	};
}
Comments bubble
When a variable goes out of scope, all member variables are deallocated recursively.
Comments bubble
When a variable goes out of scope, all member variables are deallocated recursively.