Skip to content

require! macro #68

Description

@g00nix

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions