[][src]Function tao_of_rust::ch07::structs::structs_intro

pub fn structs_intro()

结构体

Base usage: 为结构体实现Copy,字段是Copy语义

#[derive(Debug,Copy,Clone)]
struct Book<'a> {
    name: &'a str,
    isbn: i32,
    version: i32,
}
fn main(){
    let book = Book {
        name: "Rust编程之道" , isbn: 20181212, version: 1
    };
    let book2 = Book { version: 2, ..book};
    println!("{:?}",book);
    println!("{:?}",book2);
}Run

Base usage: 为结构体实现Copy,字段是移动语义

#[derive(Debug,Copy,Clone)] // Error
struct Book {
    name: String,
    isbn: i32,
    version: i32,
}
fn main(){
    let book = Book {
        name: "Rust编程之道".to_string() , isbn: 20171111, version: 1
   };
   let book2 = Book { version: 2, ..book};
   // error[E0382]: use of partially moved value: `book`
   println!("{:?}",book);
   println!("{:?}",book2);
}Run