简体   繁体   English

Solana Rust 程序 BTreeMap

[英]Solana Rust program BTreeMap

I have read this article here and I understood that HashMap is not usable in Solana, thus, I need to use BTreeMap.我在这里阅读了这篇文章,我了解到 HashMap 在 Solana 中不可用,因此我需要使用 BTreeMap。 I am a beginner in Rust and I am having an error with the following code when trying to move from Ethereum to Solana:我是 Rust 的初学者,在尝试从以太坊迁移到 Solana 时出现以下代码错误:

pub fn constructor (
   let mut DomainsToIndex = BTreeMap::new();
   Domains[] pub DomainList;
   
   contractOwner = msg.sender;
   firstDomain.name = "empty";
   firstDomain.IP = "n/a";
   firstDomain.owner = 0;
   firstDomain.lockTime = 0;
   firstDomain.infoDocumentHash = "n/a";

   DomainsToIndex.insert(String::from(firstDomain.name), 0);
   DomainList.push(firstDomain);
) -> ProgramResult {
   msg!("First domain was added by default");
   Ok(())
} 

I of course added the import in the top of the file with:我当然在文件顶部添加了导入:

use std::collections::BTreeMap;

The error I receive when using cargo build is the following as per the image presented below:根据下图,我在使用cargo build时收到的错误如下:

货物 BTreeMap 错误

I presume that I am not doing something right, as I am a newbie in Rust, can you please help out?我想我做的不对,因为我是 Rust 的新手,你能帮忙吗?

Thanks.谢谢。

There are a couple of syntactical issues with the code.代码有几个语法问题。 Application arguments should be separate from the body and pub without a struct doesn't make sense either.应用程序 arguments 应该与正文分开,没有结构的pub也没有意义。

Unfortunately the documentation of their Rust interface is quite lacking (seems to be mostly "have a look at some examples then find out the rest through trial-and-error").不幸的是,他们的 Rust 接口的文档非常缺乏(似乎主要是“看看一些示例,然后通过反复试验找出 rest”)。 So I was unable to look up enough information to suggest a reasonably correct version.所以我无法查找足够的信息来建议一个合理正确的版本。

Here are a couple of more pointers:这里还有几个指针:

  • it's not clear what the input to this function is.目前尚不清楚这个 function 的输入是什么。 You're referencing a msg object with a sender member there, but the only equivalent I could identify was the &[AccountInfo] argument which identifies the invoking account.您正在引用带有sender成员的msg object,但我可以识别的唯一等效项是标识调用帐户的&[AccountInfo]参数。
  • Alternatively, Solana programs receive a byte array of instruction data which apparently can have any content encoded within them.或者,Solana 程序接收指令数据的字节数组,显然可以在其中编码任何内容。

I would suggest starting with their Hello World example, playing around with it a bit and continue with your own app once you're more familiar with Rust syntax and Solana best practices.我建议从他们的Hello World示例开始,稍微玩一下,一旦您更熟悉 Rust 语法和 Solana 最佳实践,就继续使用您自己的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM