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

使用Slint开发2048游戏遇所有权问题,求标准解决方案

解决Slint开发2048游戏中的Rust所有权冲突问题

问题分析

你遇到的Use of moved value: 'game'错误,本质是Rust所有权规则的约束:两个闭包(on_restart和on_play)都通过move关键字捕获了game,但Rust不允许同一个值被多次移动——即便这两个闭包不会同时触发,编译器也无法识别这一业务逻辑,因此抛出错误。

标准解决方法

使用**引用计数(Rc)+ 内部可变性(RefCell)**的组合,实现多个闭包对同一个游戏状态的共享可变访问,这是Rust中处理跨闭包共享可变状态的标准方案。

修改步骤

  1. 导入Rc和RefCell模块;
  2. 将Board实例包装为Rc<RefCell<Board>>,实现共享所有权与内部可变;
  3. 为每个闭包克隆一份Rc,避免所有权移动;
  4. 在闭包中通过RefCell的borrow_mut()获取可变引用修改游戏状态,通过borrow()获取不可变引用用于UI更新。

修改后的完整代码

mod board;

use board::Board;
use slint::{Weak, AppWindow};
// 导入引用计数和内部可变性模块
use std::rc::Rc;
use std::cell::RefCell;

slint::include_modules!();

fn update_app(ui: AppWindow, game: &Board) {
    // 原UI更新逻辑保持不变
    ...
}

fn main() -> Result<(), slint::PlatformError> {
    // 将game包装为Rc<RefCell<Board>>
    let game = Rc::new(RefCell::new(Board::new(32)));

    let ui = AppWindow::new()?;
    let ui_handle = ui.as_weak();

    // 初始化UI时,通过borrow()获取不可变引用
    update_app(ui_handle.unwrap(), &game.borrow());

    // 为on_restart闭包克隆Rc
    let game_restart_clone = Rc::clone(&game);
    ui.on_restart(move |seed| {
        println!("Restarting with seed: {}", seed);
        // 通过borrow_mut()获取可变引用修改状态
        game_restart_clone.borrow_mut().restart(seed.parse().unwrap());
        // 获取不可变引用更新UI
        update_app(ui_handle.unwrap(), &game_restart_clone.borrow());
    });

    // 为on_play闭包克隆Rc
    let game_play_clone = Rc::clone(&game);
    ui.on_play(move |direction| {
        println!("Playing with direction: {:?}", direction);
        game_play_clone.borrow_mut().apply_action(direction as u8);
        update_app(ui_handle.unwrap(), &game_play_clone.borrow());
    });

    ui.run()
}

方案说明

  • Rc<Board>:允许多个闭包共享同一个Board实例的所有权,每个Rc克隆都是对同一底层数据的引用,不会发生值移动;
  • RefCell<Board>:提供内部可变性,允许在持有Rc(不可变引用)的情况下修改内部的Board实例,其借用规则在运行时检查——由于你的两个闭包不会同时触发,因此不会出现RefCell的借用 panic;
  • 每个闭包克隆独立的Rc:保证闭包各自持有一份共享引用,避免所有权冲突,同时不影响底层数据的一致性。

内容的提问来源于stack exchange,提问作者Nicolas M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 00:39:54