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

如何在Rust中遍历非可迭代的Instruction类型元素?

错误原因

Instruction是Solana SDK定义的普通结构体,未实现IntoIterator trait,无法直接作为for循环的遍历对象。从打印结果可知Instruction包含三类字段:

  • 单个值字段:program_id(程序公钥)
  • 可迭代集合字段:accounts(AccountMeta数组)、data(u8类型指令数据数组)
最简遍历方案

如果需要遍历集合类字段,直接取出对应字段遍历即可:

// 遍历账户元数据列表
for account in &create_account_instruction.accounts {
    println!("{:?}", account);
}

// 遍历指令数据字节
for byte in &create_account_instruction.data {
    println!("{:?}", byte);
}

如果只是需要查看完整结构体内容,直接使用带换行的格式化打印即可,无需额外遍历:

println!("{:#?}", create_account_instruction);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 06:57:02