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

add a macro rule system! #12915

Closed
electricface opened this issue Mar 15, 2014 · 4 comments
Closed

add a macro rule system! #12915

electricface opened this issue Mar 15, 2014 · 4 comments

Comments

@electricface
Copy link

macro_rules! system(
    ( $cmd:expr ) => (
        {
            let config = ::std::io::ProcessConfig {
                program: $cmd,
                args: &[],
                stdin: ::std::io::process::InheritFd(0),
                stdout: ::std::io::process::InheritFd(1),
                stderr: ::std::io::process::InheritFd(2),
                .. ::std::io::ProcessConfig::new()
            };

            let mut pro = match ::std::io::Process::configure(config){
                Ok(o) => o,
                Err(e) => fail!("failed to execute process: {} [{}]", $cmd ,e)
            };
            pro.wait()
        }
    );

    ( $cmd:expr,$($args:expr)* ) => (
        {
            let config = ::std::io::ProcessConfig {
                program: $cmd,
                args: &[$($args)*],
                stdin: ::std::io::process::InheritFd(0),
                stdout: ::std::io::process::InheritFd(1),
                stderr: ::std::io::process::InheritFd(2),
                .. ::std::io::ProcessConfig::new()
            };

            let mut pro = match ::std::io::Process::configure(config){
                Ok(o) => o,
                Err(e) => fail!("failed to execute process: {} [{}]", $cmd ,e)
            };
            pro.wait()
        }
    )
)

like the perl 's "system" function.

fn main(){
    let ret = system!("bc",);
    println!("{}" , ret );
}
@lifthrasiir
Copy link
Contributor

As with your other issues, I think you need some rationales to suggest those macros. And I guess it is a better fit for the function (std::io::process::run_process perhaps).

@alexcrichton
Copy link
Member

Rust's libraries greatly favor composition over specific functions such as this. As with your other issues about new macros, these are mostly conveniences and don't particularly serve any fundamental purpose.

The existing macros in libstd/macros.rs should all exist because of a fundamental reason. For example, format! could never be a library function because it takes a variable number of arguments on which is has special constraints. The macros you have proposed often do not have a fundamental reason to be macros, and would work well with simple functions instead.

For this reason, I'm going to close this and the associated issues about macros. Issues can be created about adding convenience functions if you like, but I do not believe that this functionality belongs in macros.

@electricface
Copy link
Author

i think if you add these macro to std lib ,maybe programmers can use rust like a scripting languages.

@lifthrasiir
Copy link
Contributor

@electricface Rust itself is not designed as a scripting language though. How about making your own macro library (see #11151) to provide additional macros like these?

lnicola pushed a commit to lnicola/rust that referenced this issue Aug 2, 2022
internal: Update `xtask promote` and release instructions

Update `xtask` for the subtree workflow. This doesn't explain how to do a `rust -> RA`  sync, since that's definitely more involved, but will probably only happen rarely.
flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 13, 2024
… r=xFrednet

Handle single chars with `to_string()` for `single_char_add_str`

Add support for single chars / literals with `to_string()` call for `push_str()` and `insert_str()`.

changelog: [`single_char_add_str`]: handle single chars with `to_string()` call

Closes rust-lang#12775
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

3 participants