Skip to content

add a macro rule system! #12915

Closed
Closed

Description

@electricface
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 );
}

Activity

  1. lifthrasiir commented on Mar 15, 2014

    @lifthrasiir
    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).

  2. alexcrichton commented on Mar 15, 2014

    @alexcrichton
    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.

  3. electricface commented on Mar 16, 2014

    @electricface
    Author

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

  4. lifthrasiir commented on Mar 16, 2014

    @lifthrasiir
    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?

  5. added a commit that references this issue on Aug 2, 2022
  6. added a commit that references this issue on Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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