简体   繁体   中英

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. I am a beginner in Rust and I am having an error with the following code when trying to move from Ethereum to 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:

货物 BTreeMap 错误

I presume that I am not doing something right, as I am a newbie in Rust, can you please help out?

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.

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"). 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. 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.
  • Alternatively, Solana programs receive a byte array of instruction data which apparently can have any content encoded within them.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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