zoukankan      html  css  js  c++  java
  • rust move

    // 此函数取倒堆分配的内存的所有权
    fn destroy_box(c: Box<i32>) {
        println!("Destroying a box that contains {}", c);
    
        // `c` 被销毁且内存得到释放
    }
    
    fn main() {
        // 栈分配的整型
        let x = 5u32;
    
        // 将 `x` **复制**(*Copy*)到 `y`——不存在资源移动
        let y = x;
    
        // 两个值都可以独立地使用
        println!("x is {}, and y is {}", x, y);
    
        // `a` 是一个指向堆分配的整型的指针
        let a = Box::new(5i32);
    
        println!("a contains: {}", a);
    
        // **移动**(*Move*) `a` 到 `b`
        let b = a;
        // 把 `a` 的指针地址(非数据)复制到 `b`。现在两者都是指向
        // 同一个堆分配的数据,但是现在是 `b` 占有它。
        
        // 报错!`a` 再也不能访问数据,因为它不再拥有堆上的内存。
        println!("a contains: {}", a);
        // 试一试 ^ 将此行注释去掉
    
        // 此函数从 `b` 中取得栈分配的内存的所有权
        //destroy_box(b);
    
        // 此时堆上的内存已经释放掉,而这个操作会导致解引用已释放的内存,
        // 但这种情况会被编译器会禁止。
        // 报错!和前面出错的原因一样。
        //println!("b contains: {}", b);
        // 试一试 ^ 将此行注释去掉
    }
       Compiling own v0.1.0 (/data2/rust/move)
    warning: unused variable: `b`
      --> src/main.rs:24:9
       |
    24 |     let b = a;
       |         ^ help: if this is intentional, prefix it with an underscore: `_b`
       |
       = note: `#[warn(unused_variables)]` on by default
    
    error[E0382]: borrow of moved value: `a`
      --> src/main.rs:29:32
       |
    19 |     let a = Box::new(5i32);
       |         - move occurs because `a` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
    ...
    24 |     let b = a;
       |             - value moved here
    ...
    29 |     println!("a contains: {}", a);
       |                                ^ value borrowed here after move
    
    error: aborting due to previous error; 1 warning emitted
    
    For more information about this error, try `rustc --explain E0382`.
    error: could not compile `own`.
    
    To learn more, run the command again with --verbose.
    cat src/main.rs 
    // 此函数取倒堆分配的内存的所有权
    fn destroy_box(c: Box<i32>) {
        println!("Destroying a box that contains {}", c);
    
        // `c` 被销毁且内存得到释放
    }
    
    fn main() {
        // 栈分配的整型
        let x = 5u32;
    
        // 将 `x` **复制**(*Copy*)到 `y`——不存在资源移动
        let y = x;
    
        // 两个值都可以独立地使用
        println!("x is {}, and y is {}", x, y);
    
        // `a` 是一个指向堆分配的整型的指针
        let a = Box::new(5i32);
    
        println!("a contains: {}", a);
    
        // **移动**(*Move*) `a` 到 `b`
        let b = a;
        // 把 `a` 的指针地址(非数据)复制到 `b`。现在两者都是指向
        // 同一个堆分配的数据,但是现在是 `b` 占有它。
        
        // 报错!`a` 再也不能访问数据,因为它不再拥有堆上的内存。
        //println!("a contains: {}", a);
        // 试一试 ^ 将此行注释去掉
    
        // 此函数从 `b` 中取得栈分配的内存的所有权
        destroy_box(b);
    
        // 此时堆上的内存已经释放掉,而这个操作会导致解引用已释放的内存,
        // 但这种情况会被编译器会禁止。
        // 报错!和前面出错的原因一样。
        //println!("b contains: {}", b);
        // 试一试 ^ 将此行注释去掉
    }
    [root@bogon move]# cargo build
       Compiling own v0.1.0 (/data2/rust/move)
        Finished dev [unoptimized + debuginfo] target(s) in 0.39s
    [root@bogon move]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/own`
    x is 5, and y is 5
    a contains: 5
    Destroying a box that contains 5
    cat src/main.rs 
    // 此函数取倒堆分配的内存的所有权
    fn destroy_box(c: Box<i32>) {
        println!("Destroying a box that contains {}", c);
    
        // `c` 被销毁且内存得到释放
    }
    
    fn main() {
        // 栈分配的整型
        let x = 5u32;
    
        // 将 `x` **复制**(*Copy*)到 `y`——不存在资源移动
        let y = x;
    
        // 两个值都可以独立地使用
        println!("x is {}, and y is {}", x, y);
    
        // `a` 是一个指向堆分配的整型的指针
        let a = Box::new(5i32);
    
        println!("a contains: {}", a);
    
        // **移动**(*Move*) `a` 到 `b`
        let b = a;
        // 把 `a` 的指针地址(非数据)复制到 `b`。现在两者都是指向
        // 同一个堆分配的数据,但是现在是 `b` 占有它。
        
        // 报错!`a` 再也不能访问数据,因为它不再拥有堆上的内存。
        //println!("a contains: {}", a);
        // 试一试 ^ 将此行注释去掉
    
        // 此函数从 `b` 中取得栈分配的内存的所有权
        destroy_box(b);
    
        // 此时堆上的内存已经释放掉,而这个操作会导致解引用已释放的内存,
        // 但这种情况会被编译器会禁止。
        // 报错!和前面出错的原因一样。
        println!("b contains: {}", b);
        // 试一试 ^ 将此行注释去掉
    }
     Compiling own v0.1.0 (/data2/rust/move)
    error[E0382]: borrow of moved value: `b`
      --> src/main.rs:38:32
       |
    24 |     let b = a;
       |         - move occurs because `b` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
    ...
    33 |     destroy_box(b);
       |                 - value moved here
    ...
    38 |     println!("b contains: {}", b);
       |                                ^ value borrowed here after move
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0382`.
    error: could not compile `own`.
    
    To learn more, run the command again with --verbose.
    [root@bogon move]# 
  • 相关阅读:
    HTML元素解释
    Java命名规范
    HDU 1058 Humble Numbers(DP,数)
    HDU 2845 Beans(DP,最大不连续和)
    HDU 2830 Matrix Swapping II (DP,最大全1矩阵)
    HDU 2870 Largest Submatrix(DP)
    HDU 1421 搬寝室(DP)
    HDU 2844 Coins (组合背包)
    HDU 2577 How to Type(模拟)
    HDU 2159 FATE(二维完全背包)
  • 原文地址:https://www.cnblogs.com/dream397/p/14185210.html
Copyright © 2011-2022 走看看