不懂装懂的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!("写入配置文件成功");
}
相关推荐
Albart57516 小时前
【玩转 AtomCode】替代Claude Code!开源多模型免费AI编码Agent深度实测教程
人工智能·rust·开源·atomcode·多模型ai
特立独行的猫A16 小时前
Rust+Tauri 2实现个人聚藏APP:一个 “注定上不了“ 的个人收藏夹
rust
jeffwang16 小时前
我把同一个压测做错了三次,第四次才发现真正的瓶颈
分布式·性能优化·rust
程序员爱钓鱼17 小时前
Rust 生命周期详解:生命周期标注、约束与真实项目应用
后端·面试·rust
MoonBit月兔17 小时前
MoonBit 受邀亮相 SPLASH/ISSTA 2026,与 Java、Rust、Eiffel、D 语言核心贡献者同场交流
开发语言·后端·rust
今天AI了吗17 小时前
Python vs Rust AI 服务基准测试全景:FastAPI、Axum、Actix-Web 的延迟与吞吐
人工智能·python·rust
Source.Liu18 小时前
【A11】项目架构设计笔记
rust
x-cmd18 小时前
用 Rust 打造 AI 时代的 SQL:把重复任务变成可执行文件
数据库·人工智能·sql·ai·容器·rust·workflow
铅笔侠_小龙虾2 天前
rust 学习(9):结构体
开发语言·学习·rust
铅笔侠_小龙虾2 天前
rust 学习(12):常用集合(上)-- Vec
rust