Rust常用数据结构教程 序列-三、LinkedList

时间:2024-11-07 07:00:36

通过链式存储数据的一种序列数据结构

A double-linked with owned nodes

1.什么时候用LinkedList

·根据Rust文档,LinkedList数据结构在分割/追加方面是高效的

如果您希望对分配多少内存以及何时分配内存有非常细粒度的控制,那么链表是一个不错的选择。但是绝大多数你用不到,而且你要考虑成本

use std::collections::LinkedList;

#[derive(Debug, Clone)]
struct Car {
    id: i32,
    name: String,
}
// vecque 方法都有
// vec 无
fn main() {
    let mut int_link: LinkedList<i32> = LinkedList::new();
    let mut cars = LinkedList::from([
        Car {
            id: 1,
            name: "Car-1".to_string(),
        },
        Car {
            id: 2,
            name: "Car-2".to_string(),
        },
        Car {
            id: 3,
            name: "Car-3".to_string(),
        },
    ]);
    cars.push_back(Car {
        id: 4,
        name: "Car-4".to_string(),
    });
    println!("back: {:?}", cars.back());
    cars.push_front(Car {
        id: 0,
        name: "Car-0".to_string(),
    });
    println!("front: {:?}", cars.front());
    println!("{:?}", cars);

    let car = cars.pop_back().unwrap();
    println!("{:?}", car);

    let car = cars.pop_front().unwrap();
    println!("{:?}", car);

    /**
     * split_off 是一个方法,用于将 Vec 切成两部分。它会将 cars 向量从指定的索引位置分成两部分,并返回从该位置开始的所有元素,同时将这些元素从原向量中移除。
     */
    let mut car_list = cars.split_off(cars.len() - 2);
    println!("---{:?}", car_list);
    println!("{:?}", cars);
    cars.append(&mut car_list);
    println!("{:?}", cars);
    println!("{:?}", car_list);
    cars.clear();
    println!("{}", cars.is_empty());
}

编译及运行:

 cargo run
   Compiling data_struct v0.1.0 (/home/wangji/installer/rust/data_struct/data_struct)
warning: unused doc comment
  --> src/main.rs:44:5
   |
44 | /     /**
45 | |      * split_off 是一个方法,用于将 Vec 切成两部分。它会将 cars 向量从指定的索引位置分成两部分,并返回从该位置开始的所有元素,同时将这些元素从原向量中移除。
46 | |      */
   | |_______^
47 |       let mut car_list = cars.split_off(cars.len() - 2);
   |       -------------------------------------------------- rustdoc does not generate documentation for statements
   |
   = help: use `/* */` for a plain comment
   = note: `#[warn(unused_doc_comments)]` on by default

warning: unused variable: `int_link`
  --> src/main.rs:11:13
   |
11 |     let mut int_link: LinkedList<i32> = LinkedList::new();
   |             ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_int_link`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable
  --> src/main.rs:11:9
   |
11 |     let mut int_link: LinkedList<i32> = LinkedList::new();
   |         ----^^^^^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: fields `id` and `name` are never read
 --> src/main.rs:5:5
  |
4 | struct Car {
  |        --- fields in this struct
5 |     id: i32,
  |     ^^
6 |     name: String,
  |     ^^^^
  |
  = note: `Car` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: `data_struct` (bin "data_struct") generated 4 warnings (run `cargo fix --bin "data_struct"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.13s
     Running `target/debug/data_struct`
back: Some(Car { id: 4, name: "Car-4" })
front: Some(Car { id: 0, name: "Car-0" })
[Car { id: 0, name: "Car-0" }, Car { id: 1, name: "Car-1" }, Car { id: 2, name: "Car-2" }, Car { id: 3, name: "Car-3" }, Car { id: 4, name: "Car-4" }]
Car { id: 4, name: "Car-4" }
Car { id: 0, name: "Car-0" }
---[Car { id: 2, name: "Car-2" }, Car { id: 3, name: "Car-3" }]
[Car { id: 1, name: "Car-1" }]
[Car { id: 1, name: "Car-1" }, Car { id: 2, name: "Car-2" }, Car { id: 3, name: "Car-3" }]
[]
true