-2

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:

Cargo BTreeMap error

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

Thanks.

FBK
  • 52
  • 5
  • 20
  • 3
    Is this Rust code? If so, you put code inside the function's parameters, which is simply invalid syntax. You should put all of this in the body - after `msg!(...);`, for example. – ForceBru Dec 24 '21 at 15:02
  • Yes, it is Rust. Thank you for the hint, did not know this, will try :) – FBK Dec 24 '21 at 15:21

1 Answers1

0

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.

Marcus Ilgner
  • 6,935
  • 2
  • 30
  • 44