Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require! macro #68

Open
g00nix opened this issue Apr 16, 2022 · 0 comments
Open

require! macro #68

g00nix opened this issue Apr 16, 2022 · 0 comments

Comments

@g00nix
Copy link

g00nix commented Apr 16, 2022

I see that Anchor is suggesting the use of the require! macro, which produces code like this:

    pub fn is_active(&self) -> bool {
        self.state == GameState::Active
    }
    pub fn play(&mut self, tile: &Tile) -> Result<()> {
        require!(self.is_active(), TicTacToeError::GameAlreadyOver);
        ...
    }

However this is not idiomatic Rust. Normally in Rust you would see something like this:

     fn is_active(&self) -> Result<()> {
         match self.state.eq(&GameState::Active) {
             false => Err(TicTacToeError::GameAlreadyOver),
             true => Ok(()),
         }
     }


     pub fn play(&mut self, tile: &Tile) -> Result<()> {
         self.is_active()?;
         ...
     }

I feel like this is an attempt to bring the Solidity coding style into Rust. Is there a technical reason to this, or is this popular just because a lot of smart contract devs are transitioning from Solidity to Rust?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant