不懂装懂的AI,折了程序员的阳寿

记录一次AI让我砸键盘的折寿行为。

最近在用 AI 来辅助学习 Rust 编程,想完成一个功能,读取 config.toml 配置文件,内容格式如下:

toml 复制代码
[dev]
username = "tester1"
password = "******"
[prod]
username = "tester2"
password = "******"

功能需要满足以下要求:

  • 如果能找到对应的配置,写入时直接更新
  • 如果找不到,则创建一个新的配置块,写入到文件最后
  • 新插入的配置块不能影响原有的配置顺序

实现了如下接口 write_target ,使用的是 toml 这个库,但是这个库做序列化写入的时候不能满足第三点需求(顺序是变的)

咨询AI,说是可以用 toml_edit 来实现这一要求,于是把上面的需求作为提示词给了 AI,让他用 toml_edit 帮我重新实现 write_target 这个函数。

经过几十轮的修改,最终还是没实现,代码都编译不过。(主要原因还是我菜,此处:狗头保命)

尝试过 Trae, Cursor,都没有一个能写出来通过编译的。(此处,跪求掘金大佬指点)

rust 复制代码
#[derive(Serialize, Deserialize, Debug, Default)]
struct Config {
    #[serde(flatten)]
    targets: std::collections::HashMap<String, Target>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Target {
    username: String,
    password: String,
}

fn write_target(target: &String, username: &String, password: &String) {
    let mut config_path = home_dir().expect("无法访问Home目录");
    config_path.push(".config/yt/config.toml");
    let mut config: Config;
    if config_path.exists() {
        let content = fs::read_to_string(&config_path).unwrap();
        config = toml::from_str(&content).unwrap();
    } else {
        println!("配置文件不存在");
        fs::create_dir_all(config_path.parent().unwrap()).unwrap();
        config = Config::default();
    }

    let item = config.targets.get_mut(target);
    match item {
        Some(target) => {
            target.username = username.clone();
            target.password = password.clone();
        }
        None => {
            config.targets.insert(target.clone(), Target { username: username.clone(), password: password.clone() });
        }
    }

    match fs::write(config_path, toml::to_string(&config).unwrap()) {
        Ok(_) => {
            println!("写入配置文件成功");
        }
        Err(e) => {
            eprintln!("写入配置文件失败: {}", e);
        }
    }
}

后面翻 toml_edit 的文档:crates.io/crates/toml..., 看到示例中使用了 DocumentMut,于是亲自动手来修改 write_taget方法,居然成功了。

简化后的代码:

rust 复制代码
fn write_target(target: &String, username: &String, password: &String) {
    let mut config_path = home_dir().expect("无法访问Home目录");
    config_path.push(".config/yt/config.toml");

    let content = if config_path.exists() {
        fs::read_to_string(&config_path).unwrap()
    } else {
        String::new()
    };

    let mut doc = content.parse::<DocumentMut>().unwrap();
    let mut new_table = Table::new();
    new_table.insert("username", Item::Value(Value::from(username)));
    new_table.insert("password", Item::Value(Value::from(password)));
    doc[target] = Item::Table(new_table);

    fs::write(config_path, doc.to_string()).unwrap();
    println!("写入配置文件成功");
}
相关推荐
doiito8 小时前
【Agent Harness】Gliding Horse 给 Agent OS 装上双曲空间引擎与默克尔树边云同步
ai·rust·架构设计·系统设计·ai agent
doiito1 天前
【Agent Harness】Gliding Horse 本体论系统设计:给 AI Agent 装上“语义大脑”
ai·rust·架构设计·系统设计·ai agent
大卫小东(Sheldon)2 天前
Rust 推荐使用宏而非普通函数的场景
rust
doiito2 天前
【Agent Harness】为什么我把 JSON‑LD “编译成 DAG” 后,整个 Agent 平台立刻聪明了
ai·rust·架构设计·系统设计·ai agent
jump_jump3 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
星栈4 天前
Dioxus 多页面怎么做:`dioxus-router`、嵌套路由、`Outlet` 和页面组织,一篇给你讲顺
前端·rust·前端框架
Rust研习社5 天前
组合真的优于继承吗?为什么 Rust 和 Go 都拥抱组合舍弃继承?
后端·rust·编程语言
红尘散仙6 天前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust
vivo互联网技术6 天前
从 Web 到桌面:基于 Tauri 2.0 + Vue 3 打造 vivo 线下门店「大头贴」拍照体验系统
前端·rust