01-Box

Box — Rust By Practice

内容来源 · Rust By Practice / Rust 语言实战

原文链接: https://practice-rust.beatai.org/smart-pointers/box.html

Box

  1. 🌟
1
2
3
4
5
6
7
// 让代码工作
fn main() {
    // 创建一个包含整数 5 的新 box `b`
    assert_eq!(*b, 5);

    println!("Success!");
}
  1. 🌟
 1
 2
 3
 4
 5
 6
 7
 8
 9
10

// 让代码工作
fn main() {
    let b = Box::new("Hello");
    print_boxed_string(b);
}

fn print_boxed_string(b : _) {
    println!("{}", b);
}
  1. 🌟
1
2
3
4
5
6
7
8
9

// 让代码工作
fn main() {
    let b1 = Box::new(5);
    let b2 = b1;
    assert_eq!(_, 5);

    println!("Success!");
}
  1. 🌟
1
2
3
4
5
6

// 让代码工作
fn main() {
    // 创建一个包含数组 [1, 2, 3, 4, 5] 的 box `b`
    // 打印 `b` 中的每个整数
}

你可以在这里找到答案(在 solutions 路径下),但请只在需要时使用

最后修改 August 23, 2026: 更新 (499855b16)