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

基于Iced的Rust GUI方程求解程序编译报错排查求助

问题:使用Iced框架开发方程求解GUI程序时的编译错误

我想用Iced框架开发一款带GUI的方程求解程序,预期程序能正常运行,但实际编译报错。


首次报错(完整栈跟踪)

error[E0593]: closure is expected to take 2 arguments, but it takes 1 argument
  --> hello-world-iced/src/main.rs:4:5
   |
4 |     iced::application(SolveLinearEquation::new, |message| iced::Task::none(),|state: &SolveLinearEquation| state.view()).run()
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |                                           |
   |     |                                           takes 1 argument
   |     expected closure that takes 2 arguments
   |
   = note: required for `{closure@hello-world-iced/src/main.rs:4:49: 4:58}` to implement `iced::application::UpdateFn<_, _>`
note: required by a bound in `application`
  --> /home/ratratarmy/.cargo/git/checkouts/iced-3e930cb38a75fe30/d4f568a/src/application.rs:80:18
   |
78 | pub fn application<State, Message, Theme, Renderer>(
   |        ----------- required by a bound in this function
79 |     boot: impl BootFn<State, Message>,
80 |     update: impl UpdateFn<State, Message>,
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `application`

对应代码

src/main.rs

mod gui;
use crate::gui::{SolveLinearEquation, SolveLinearEquationMessage};
fn main() -> iced::Result {
    iced::application(SolveLinearEquation::new, |message| iced::Task::none(),|state: &SolveLinearEquation| state.view()).run()
}

src/gui/mod.rs

pub mod solve_linear_equation;
pub use solve_linear_equation::{SolveLinearEquation, SolveLinearEquationMessage};

src/gui/solve_linear_equation.rs

use {
    iced::widget::{button, column, text_input, text, Column},
    equation_solver::solve_linear_equation
};
pub struct SolveLinearEquation {
    pub(crate) a: f64,
    pub(crate) b: f64,
    pub(crate) res: Option<f64>
}
#[derive(Clone)]
pub enum SolveLinearEquationMessage {
    AChanged(f64),
    BChanged(f64),
    SolveEquation
}
impl SolveLinearEquation {
    pub(crate) fn new() -> (Self, iced::Task<SolveLinearEquationMessage>) {
        (
            SolveLinearEquation {a: 0f64, b: 0f64, res: None},
            iced::Task::none()
        )
    }
    pub fn update(&mut self, message: SolveLinearEquationMessage) {
        match message {
            SolveLinearEquationMessage::AChanged(value) => self.a = value,
            SolveLinearEquationMessage::BChanged(value) => self.b = value,
            SolveLinearEquationMessage::SolveEquation => self.res = solve_linear_equation(self.a, self.b)
        }
    }
    pub fn view(&self) -> Column<'_, SolveLinearEquationMessage> {
        column![
            text("Giải phương trình ax + b = 0 (a != 0)"),
            text_input("Nhập số a: ", &self.a.to_string()).on_input(|s| {
                s.parse::<f64>()
                .map(SolveLinearEquationMessage::AChanged)
                .unwrap_or(SolveLinearEquationMessage::AChanged(self.a))
            }),
            text_input("Nhập số b: ", &self.a.to_string()).on_input(|s| {
                s.parse::<f64>().map(SolveLinearEquationMessage::BChanged)
                .unwrap_or(SolveLinearEquationMessage::BChanged(self.b))
            }),
            button("Giải phương trình ax + b = 0 (a != 0)").on_press(SolveLinearEquationMessage::SolveEquation),
            text(self.res.unwrap())
        ].into()
    }
}

修复后出现的新报错

调整代码修复第一个错误后,又遇到两个编译错误:

错误1

error: lifetime may not live long enough
 --> hello-world-iced/src/main.rs:7:37
  |
7 |     },|state: &SolveLinearEquation| state.view()).run()
  |               -                   - ^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
  |               |                   |
  |               |                   return type of closure is iced::widget::Column<'2, SolveLinearEquationMessage>
  |               let's call the lifetime of this reference `'1`

错误2

error: implementation of `iced::application::ViewFn` is not general enough
 --> hello-world-iced/src/main.rs:4:5
  |
4 | /     iced::application(SolveLinearEquation::new, |state: &mut SolveLinearEquation, message| { // Takes 2 arguments
5 | |         state.update(message); // Call your method
6 | |         iced::Task::none()
7 | |     },|state: &SolveLinearEquation| state.view()).run()
  | |_______________________________________________________^ implementation of `iced::application::ViewFn` is not general enough
  |
  = note: `{closure@hello-world-iced/src/main.rs:7:7: 7:36}` must implement `iced::application::ViewFn<'0, SolveLinearEquation, SolveLinearEquationMessage, Theme, iced_renderer::fallback::Renderer<iced_wgpu::Renderer, iced_tiny_skia::Renderer>>`, for any lifetime `'0`...
  = note: ...but it actually implements `iced::application::ViewFn<'1, SolveLinearEquation, SolveLinearEquationMessage, Theme, iced_renderer::fallback::Renderer<iced_wgpu::Renderer, iced_tiny_skia::Renderer>>`, for some specific lifetime `'1`

对应修改后的代码

src/main.rs

mod gui;
use crate::gui::{SolveLinearEquation, SolveLinearEquationMessage};
fn main() -> iced::Result {
    iced::application(SolveLinearEquation::new, |state: &mut SolveLinearEquation, message| { // Takes 2 arguments
        state.update(message); // Call your method
        iced::Task::none()
    },|state: &SolveLinearEquation| state.view()).run()
}

src/gui/mod.rs 和 src/gui/solve_linear_equation.rs 未改动


环境信息

  • Rust版本: 1.97.0-nightly (e96c36b6f 2026-05-21) (rustc), 1.97.0-nightly (4d1f98451 2026-05-15) (cargo)
  • Iced crate版本: 0.15.0-dev(基于d4f568a版本)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.02 04:37:26