如何迭代不可迭代的 Rust 元素?
How to iterate in a Rust element non-iterable?
我正在尝试使用 for 循环遍历从 API 创建的这个变量的结果(此处已注释,否则会出错):
let create_account_instruction: Instruction = solana_sdk::system_instruction::create_account(
&wallet_pubkey,
&mint_account_pubkey,
minimum_balance_for_rent_exemption,
Mint::LEN as u64,
&spl_token::id(),
);
println!("Creating the following instructions: {:?}", create_account_instruction);
// for x in create_account_instruction {
// println!("{:?}", x)
// }
这是我想要迭代的结果(仅供参考:那些私钥和 public 密钥仅用于在 devnet 上进行测试):
Creating the following instructions: Instruction { program_id: 11111111111111111111111111111111, accounts: [AccountMeta { pubkey: ESCkgk5AfDC8cXd4KYjkUda1psCL8otfu8NvniUBiGhX, is_signer: true, is_writable: true }, AccountMeta { pubkey: Ah63GoKnnBicTELvfz2F9YvF9vaR51HR2BK3hJWwWE8x, is_signer: true, is_writable: true }], data: [0, 0, 0, 0, 96, 77, 22, 0, 0, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169] }
如果我尝试通过 for 循环遍历它(取消上面的注释),我得到这个错误:
Compiling AmatoRaptor v0.1.0 (/home/joomjoo/Desktop/Tester)
error[E0277]: `Instruction` is not an iterator
--> src/main.rs:89:14
|
89 | for x in create_account_instruction {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Instruction` is not an iterator
|
= help: the trait `Iterator` is not implemented for `Instruction`
= note: required because of the requirements on the impl of `IntoIterator` for `Instruction`
note: required by `into_iter`
--> /home/joomjoo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:234:5
|
234 | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain
我的问题是迭代结果的最简单方法是什么?
来自Solana documentation,Instruction
是一个结构:
pub struct Instruction {
pub program_id: Pubkey,
pub accounts: Vec<AccountMeta, Global>,
pub data: Vec<u8, Global>,
}
您可以访问它的属性,并迭代它们:
for x in create_account_instruction.data {
...
}
或
for x in create_account_instruction.accounts {
...
}
我正在尝试使用 for 循环遍历从 API 创建的这个变量的结果(此处已注释,否则会出错):
let create_account_instruction: Instruction = solana_sdk::system_instruction::create_account(
&wallet_pubkey,
&mint_account_pubkey,
minimum_balance_for_rent_exemption,
Mint::LEN as u64,
&spl_token::id(),
);
println!("Creating the following instructions: {:?}", create_account_instruction);
// for x in create_account_instruction {
// println!("{:?}", x)
// }
这是我想要迭代的结果(仅供参考:那些私钥和 public 密钥仅用于在 devnet 上进行测试):
Creating the following instructions: Instruction { program_id: 11111111111111111111111111111111, accounts: [AccountMeta { pubkey: ESCkgk5AfDC8cXd4KYjkUda1psCL8otfu8NvniUBiGhX, is_signer: true, is_writable: true }, AccountMeta { pubkey: Ah63GoKnnBicTELvfz2F9YvF9vaR51HR2BK3hJWwWE8x, is_signer: true, is_writable: true }], data: [0, 0, 0, 0, 96, 77, 22, 0, 0, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169] }
如果我尝试通过 for 循环遍历它(取消上面的注释),我得到这个错误:
Compiling AmatoRaptor v0.1.0 (/home/joomjoo/Desktop/Tester)
error[E0277]: `Instruction` is not an iterator
--> src/main.rs:89:14
|
89 | for x in create_account_instruction {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Instruction` is not an iterator
|
= help: the trait `Iterator` is not implemented for `Instruction`
= note: required because of the requirements on the impl of `IntoIterator` for `Instruction`
note: required by `into_iter`
--> /home/joomjoo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:234:5
|
234 | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain
我的问题是迭代结果的最简单方法是什么?
来自Solana documentation,Instruction
是一个结构:
pub struct Instruction {
pub program_id: Pubkey,
pub accounts: Vec<AccountMeta, Global>,
pub data: Vec<u8, Global>,
}
您可以访问它的属性,并迭代它们:
for x in create_account_instruction.data {
...
}
或
for x in create_account_instruction.accounts {
...
}