rust - 无法在Rust中的程序宏函数声明中打印

标签 rust

我正在使用proc_macro,并希望打印一些详细信息以进行调试。 println!语句不显示任何内容。

这是宏调用:

decl_module! {

    /// The module declaration.
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        // A default function for depositing events
        fn deposit_event() = default;

        /// Allow a user to claim ownership of an unclaimed proof
        fn create_claim(origin, proof: Vec<u8>) -> DispatchResult {
            // Verify that the incoming transaction is signed and store who the
            // caller of this function is.
            let sender = ensure_signed(origin)?;
            println!("send is: {}", sender);

            // Verify that the specified proof has not been claimed yet or error with the message
            ensure!(!Proofs::<T>::exists(&proof), "This proof has already been claimed.");

            // Call the `system` pallet to get the current block number
            let current_block = <system::Module<T>>::block_number();

            // Store the proof with the sender and the current block number
            Proofs::<T>::insert(&proof, (sender.clone(), current_block));

            // Emit an event that the claim was created
            Self::deposit_event(RawEvent::ClaimCreated(sender, proof));
            Ok(())
        }

        /// Allow the owner to revoke their claim
        fn revoke_claim(origin, proof: Vec<u8>) -> DispatchResult {
            // Determine who is calling the function
            let sender = ensure_signed(origin)?;

            // Verify that the specified proof has been claimed
            ensure!(Proofs::<T>::exists(&proof), "This proof has not been stored yet.");

            // Get owner of the claim
            let (owner, _) = Proofs::<T>::get(&proof);

            // Verify that sender of the current call is the claim owner
            ensure!(sender == owner, "You must own this claim to revoke it.");

            // Remove claim from storage
            Proofs::<T>::remove(&proof);

            // Emit an event that the claim was erased
            Self::deposit_event(RawEvent::ClaimRevoked(sender, proof));
            Ok(())
        }
    }
}


它取自here。我添加了以下行:
println!("send is: {}", sender);

我正在终端(或任何地方)中运行区块链(Polkadot dApp),我看不到输出消息。注意:一切正常,但我无法打印。

最佳答案

此宏调用生成包含打印语句的代码。它不会运行该代码。在运行代码并调用create_claim之前,您将看不到打印输出。

如果您想调试宏调用,请参见示例中的几个tools宏,但我不知道它们是否也适用于过程宏或是否具有等效项。

关于rust - 无法在Rust中的程序宏函数声明中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60324053/

相关文章:

rust - 将 8 个连续字节转换为半字节(以 32 位整数编码)的最快方法

rust - 为另一个特征实现一个特征是什么意思?

rust - 无法返回对 flat_map 中临时值的引用

rust - 如何通过负偏移量寻址数组索引?

rust - 如何将动态值传递到 WGPU 顶点着色器?

generics - 如何将特征绑定(bind)到非泛型类型?

rust - 为什么 Pin::map_unchecked 不安全?

rust - 使用Yew框架进行Webassembly的未发现的错误

asynchronous - 如何将 Future 转换为 Stream?

rust - 为 Rust 编写 C 库绑定(bind)的目的是什么?