You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust编译报错:类型参数State未被使用,如何解决?

解决Rust中未使用泛型参数的编译错误

我正在阅读《The Rust Book》第17章,认为该章最后的演示代码非常出色,于是尝试将其简化为如下Rust代码:

struct Draft;
struct PendingReview;
struct Approved;

struct Post<State> {
    content: String,
}

impl Post<Draft> {
    fn new() -> Post<Draft> {
        Post {
            content: String::new(),
        }
    }

    fn add_text(&mut self, text: &str) {
        self.content.push_str(text)
    }

    fn request_review(self) -> Post<PendingReview> {
        Post {
            content: self.content,
        }
    }
}

impl Post<PendingReview> {
    fn approve(self) -> Post<Approved> {
        Post {
            content: self.content,
        }
    }
}

impl Post<Approved> {
    fn content(&self) -> &str {
        &self.content
    }
}

fn main() {
    let mut post = Post::new();

    post.add_text("I ate a salad for lunch today");

    let post = post.request_review();

    let post = post.approve();

    assert_eq!("I ate a salad for lunch today", post.content());
}

执行代码时出现如下编译错误:

hello:master* λ cargo run -q
error[E0392]: parameter `State` is never used
 --> src/main.rs:5:13
  |
5 | struct Post<State> {
  |             ^^^^^ unused parameter
  |
  = help: consider removing `State`, referring to it in a field, or using a marker such as `PhantomData`
  = help: if you intended `State` to be a const parameter, use `const State: usize` instead

For more information about this error, try `rustc --explain E0392`.
error: could not compile `hello` due to previous error

修改方案

错误原因是泛型参数State仅作为状态标记使用,但未在Post结构体的字段中被引用,Rust编译器不允许存在未使用的泛型参数。解决方法是使用PhantomData标记该泛型参数,它不会占用实际内存,仅用于告知编译器该泛型参数是被使用的。

修改后的代码如下:

use std::marker::PhantomData;

struct Draft;
struct PendingReview;
struct Approved;

struct Post<State> {
    content: String,
    state: PhantomData<State>, // 添加PhantomData字段标记泛型参数
}

impl Post<Draft> {
    fn new() -> Post<Draft> {
        Post {
            content: String::new(),
            state: PhantomData,
        }
    }

    fn add_text(&mut self, text: &str) {
        self.content.push_str(text)
    }

    fn request_review(self) -> Post<PendingReview> {
        Post {
            content: self.content,
            state: PhantomData,
        }
    }
}

impl Post<PendingReview> {
    fn approve(self) -> Post<Approved> {
        Post {
            content: self.content,
            state: PhantomData,
        }
    }
}

impl Post<Approved> {
    fn content(&self) -> &str {
        &self.content
    }
}

fn main() {
    let mut post = Post::new();

    post.add_text("I ate a salad for lunch today");

    let post = post.request_review();

    let post = post.approve();

    assert_eq!("I ate a salad for lunch today", post.content());
}

关键修改点

  1. 导入std::marker::PhantomData;
  2. 在Post结构体中添加state: PhantomData<State>字段;
  3. 在所有创建Post实例的方法(new、request_review、approve)中初始化state为PhantomData。

这样修改后,编译器会认可泛型参数State的存在,代码即可正常编译运行,同时保留原有的状态机逻辑。

内容的提问来源于stack exchange,提问作者xm lian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:05:28