rust (53) - Smart Pointers

1, a default value is assigned to the stack memory, by packing Box partitioned heap memory
2, Box is a pointer to the type of smart memory allocation value T of the stack.
3, Box over scope, its destructor is invoked, deleting its internal object, automatically release the heap memory.
4, the character is acquired by dereferencing Box
. 5, Box acts as a reference, it can be automatically released.

fn main() {
	struct BookInfo{
		name:&'static str,
		price:f32,
		count:i32,
	}
	impl BookInfo{
	    fn print_book_info(&self){
	        println!("{}:{} {}",self.name,self.count,self.price); 
	    }		
	}
	let book1=Box::new(BookInfo{name:"C++模式",price:39.12,count:7});
    let unboxed_book:BookInfo=*book1;
    unboxed_book.print_book_info();

}
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `F:\learn\rustlearn\learn50\target\debug\learn50.exe`
C++模式:7 39.12


------------------
(program exited with code: 0)

请按任意键继续. . .
Published 473 original articles · won praise 14 · views 60000 +

Guess you like

Origin blog.csdn.net/AI_LX/article/details/105099360