简体   繁体   English

为什么我的代码在 use std::io::Write 在我的 main 中但在我的 lib 中时不起作用

[英]Why does my code work when use std::io::Write is in my main but not when it's in my lib

I have two files I'm working with, main.rs and lib.rs.我有两个正在使用的文件,main.rs 和 lib.rs。

main.rs:主.rs:

use std::io;
use std::io::Write; //My library depends on this line :(

use input_macro_attempt::input;

fn main() {
    let x = input!("Enter a variable: ");

    println!("The variable is: {}", x);

}

lib.rs:库.rs:

use std::io;
use std::io::Write; //This one is irrelevant?

#[macro_export]
macro_rules! input{
    ($a:expr) =>{
       {
        
        print!("{}", $a);
        std::io::stdout().flush().unwrap();
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        input
       }
    };

   (_) =>{{
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        
        input
        }
    };


}

I've experimented but when I remove the use "std::io::Write" from main.rs my code breaks but why?我已经尝试过,但是当我从 main.rs 中删除使用“std::io::Write”时,我的代码中断了,但为什么呢?

I didn't like how the output came out with println.我不喜欢 output 是如何与 println 一起出现的。 so I switched to print!所以我切换到打印! and manually flushed it so that you could put the input on the same line as the message.并手动刷新它,以便您可以将输入与消息放在同一行。

Example: Enter the input: Your input示例:输入输入:您的输入

I add it to my lib.rs and it doesn't make a difference, it only works if my main.rs has it and I'm a little frustrated because in my lib it ignores the whole use term.我将它添加到我的lib.rs中,它没有什么区别,它只有在我的main.rs有它时才有效,我有点沮丧,因为在我的 lib 中它忽略了整个使用术语。

In Rust as in other languages macro expansion is just a sort of text replacement mechanism, (well, ok Rust also checks token types etc but for your question we can ignore that) so you can think of input:("Enter a variable: ") as being replaced with the contents of the macro at the call site.在 Rust 中,与其他语言一样,宏扩展只是一种文本替换机制,(好吧,Rust 也检查令牌类型等,但对于你的问题,我们可以忽略它)所以你可以想到input:("Enter a variable: ")在调用站点被替换为宏的内容。 If you want to use an include, you have to include it within the macro itself, not the module it's defined in.如果你想使用包含,你必须将它包含在宏本身中,而不是它定义的模块中。

#[macro_export]
macro_rules! input {
    ($a:expr) => {{
        use ::std::io::Write;

        print!("{}", $a);
        ::std::io::stdout().flush().unwrap();
        let mut input = String::new();
        ::std::io::stdin().read_line(&mut input).unwrap();
        input
    }};

    (_) => {{
        use ::std::io::Write;

        let mut input = String::new();
        ::std::io::stdin().read_line(&mut input).unwrap();

        input
    }};
}

I use :: in front of the use and other std elements to guarantee I'm not accidentially including a submodule std of the call site.我在use和其他std元素之前使用::来保证我不会意外地包含调用站点的子模块std

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新“rustc”后,“use std::io”可以破坏我的代码吗? - Can `use std::io` break my code after update of `rustc`? 为什么 rustc compile 抱怨我的简单代码“特征 std::io::Read 没有为 Result 实现<File, anyhow::Error> ” - why rustc compile complain my simple code “the trait std::io::Read is not implemented for Result<File, anyhow::Error>” 为我的可写类型使用 fmt::Write 或 io::Write 特征? - Use fmt::Write or io::Write trait for my writable type? 当我删除边界检查时,为什么我的代码运行速度较慢? - Why does my code run slower when I remove bounds checks? 为什么我的套接字读取锁定了数据的写入? - Why does my socket read lock the write of the data? 代码可以正常复制/粘贴到我的 main.rs 中,但在从自己的外部 crate 运行时会被忽略 - Code works fine copy/pasted into my main.rs, but is ignored when run from its own external crate 为什么我的汉明重量函数在C中工作但在Rust中不起作用? - Why does my Hamming weight function work in C but not in Rust? 为什么我从 VS Code 对我的 Rust 程序所做的更改在我编译时没有反映出来? - Why are the changes I make to my Rust program from VS Code not reflected when I compile? 为什么`cargo build`没有在我的代码中显示所有错误? - Why does `cargo build` not show all errors in my code? 当我使用 64 位无符号整数而不是 32 位时,为什么我的 rust 程序无法编译? - Why doesnt my rust program compile when i use 64 bit unsigned integers instead of 32 bit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM