Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > fdedb3cf2140df9b6d419a2338ab1caa > files > 5546

rust-doc-1.25.0-1.mga6.x86_64.rpm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="rustdoc">
    <meta name="description" content="API documentation for the Rust `process` mod in crate `std`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, process">

    <title>std::process - Rust</title>

    <link rel="stylesheet" type="text/css" href="../../normalize.css">
    <link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle">
    
    <link rel="stylesheet" type="text/css" href="../../dark.css">
    <link rel="stylesheet" type="text/css" href="../../main.css" id="themeStyle">
    <script src="../../storage.js"></script>
    

    <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico">
    
</head>
<body class="rustdoc mod">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    

    <nav class="sidebar">
        <div class="sidebar-menu">&#9776;</div>
        <a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
        <p class='location'>Module process</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'process', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div>
    </nav>

    <div class="theme-picker">
        <button id="theme-picker" aria-label="Pick another theme!">
            <img src="../../brush.svg" width="18" alt="Pick another theme!">
        </button>
        <div id="theme-choices"></div>
    </div>
    <script src="../../theme.js"></script>
    <nav class="sub">
        <form class="search-form js-only">
            <div class="search-container">
                <input class="search-input" name="search"
                       autocomplete="off"
                       placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
                       type="search">
            </div>
        </form>
    </nav>

    <section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>process</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'>
                   <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
                       [<span class='inner'>&#x2212;</span>]
                   </a>
               </span><a class='srclink' href='../../src/std/process.rs.html#11-1852' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A module for working with processes.</p>
<p>This module is mostly concerned with spawning and interacting with child
processes, but it also provides <a href="fn.abort.html"><code>abort</code></a> and <a href="fn.exit.html"><code>exit</code></a> for terminating the
current process.</p>
<h1 id="spawning-a-process" class="section-header"><a href="#spawning-a-process">Spawning a process</a></h1>
<p>The <a href="struct.Command.html"><code>Command</code></a> struct is used to configure and spawn processes:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::<span class="ident">Command</span>;

<span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">&quot;echo&quot;</span>)
                     .<span class="ident">arg</span>(<span class="string">&quot;Hello world&quot;</span>)
                     .<span class="ident">output</span>()
                     .<span class="ident">expect</span>(<span class="string">&quot;Failed to execute command&quot;</span>);

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b&quot;Hello world\n&quot;</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3ACommand%3B%0A%0Alet%20output%20%3D%20Command%3A%3Anew(%22echo%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.arg(%22Hello%20world%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.output()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.expect(%22Failed%20to%20execute%20command%22)%3B%0A%0Aassert_eq!(b%22Hello%20world%5Cn%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre>
<p>Several methods on <a href="struct.Command.html"><code>Command</code></a>, such as <a href="struct.Command.html#method.spawn"><code>spawn</code></a> or <a href="struct.Command.html#method.output"><code>output</code></a>, can be used
to spawn a process. In particular, <a href="struct.Command.html#method.output"><code>output</code></a> spawns the child process and
waits until the process terminates, while <a href="struct.Command.html#method.spawn"><code>spawn</code></a> will return a <a href="struct.Child.html"><code>Child</code></a>
that represents the spawned child process.</p>
<h1 id="handling-io" class="section-header"><a href="#handling-io">Handling I/O</a></h1>
<p>The <a href="struct.Command.html#method.stdout"><code>stdout</code></a>, <a href="struct.Command.html#method.stdin"><code>stdin</code></a>, and <a href="struct.Command.html#method.stderr"><code>stderr</code></a> of a child process can be
configured by passing an <a href="struct.Stdio.html"><code>Stdio</code></a> to the corresponding method on
<a href="struct.Command.html"><code>Command</code></a>. Once spawned, they can be accessed from the <a href="struct.Child.html"><code>Child</code></a>. For
example, piping output from one command into another command can be done
like so:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::{<span class="ident">Command</span>, <span class="ident">Stdio</span>};

<span class="comment">// stdout must be configured with `Stdio::piped` in order to use</span>
<span class="comment">// `echo_child.stdout`</span>
<span class="kw">let</span> <span class="ident">echo_child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">&quot;echo&quot;</span>)
    .<span class="ident">arg</span>(<span class="string">&quot;Oh no, a tpyo!&quot;</span>)
    .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>())
    .<span class="ident">spawn</span>()
    .<span class="ident">expect</span>(<span class="string">&quot;Failed to start echo process&quot;</span>);

<span class="comment">// Note that `echo_child` is moved here, but we won&#39;t be needing</span>
<span class="comment">// `echo_child` anymore</span>
<span class="kw">let</span> <span class="ident">echo_out</span> <span class="op">=</span> <span class="ident">echo_child</span>.<span class="ident">stdout</span>.<span class="ident">expect</span>(<span class="string">&quot;Failed to open echo stdout&quot;</span>);

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">sed_child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">&quot;sed&quot;</span>)
    .<span class="ident">arg</span>(<span class="string">&quot;s/tpyo/typo/&quot;</span>)
    .<span class="ident">stdin</span>(<span class="ident">Stdio</span>::<span class="ident">from</span>(<span class="ident">echo_out</span>))
    .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>())
    .<span class="ident">spawn</span>()
    .<span class="ident">expect</span>(<span class="string">&quot;Failed to start sed process&quot;</span>);

<span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">sed_child</span>.<span class="ident">wait_with_output</span>().<span class="ident">expect</span>(<span class="string">&quot;Failed to wait on sed&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b&quot;Oh no, a typo!\n&quot;</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3A%7BCommand%2C%20Stdio%7D%3B%0A%0A%2F%2F%20stdout%20must%20be%20configured%20with%20%60Stdio%3A%3Apiped%60%20in%20order%20to%20use%0A%2F%2F%20%60echo_child.stdout%60%0Alet%20echo_child%20%3D%20Command%3A%3Anew(%22echo%22)%0A%20%20%20%20.arg(%22Oh%20no%2C%20a%20tpyo!%22)%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22Failed%20to%20start%20echo%20process%22)%3B%0A%0A%2F%2F%20Note%20that%20%60echo_child%60%20is%20moved%20here%2C%20but%20we%20won't%20be%20needing%0A%2F%2F%20%60echo_child%60%20anymore%0Alet%20echo_out%20%3D%20echo_child.stdout.expect(%22Failed%20to%20open%20echo%20stdout%22)%3B%0A%0Alet%20mut%20sed_child%20%3D%20Command%3A%3Anew(%22sed%22)%0A%20%20%20%20.arg(%22s%2Ftpyo%2Ftypo%2F%22)%0A%20%20%20%20.stdin(Stdio%3A%3Afrom(echo_out))%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22Failed%20to%20start%20sed%20process%22)%3B%0A%0Alet%20output%20%3D%20sed_child.wait_with_output().expect(%22Failed%20to%20wait%20on%20sed%22)%3B%0Aassert_eq!(b%22Oh%20no%2C%20a%20typo!%5Cn%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre>
<p>Note that <a href="struct.ChildStderr.html"><code>ChildStderr</code></a> and <a href="struct.ChildStdout.html"><code>ChildStdout</code></a> implement <a href="../io/trait.Read.html"><code>Read</code></a> and
<a href="struct.ChildStdin.html"><code>ChildStdin</code></a> implements <a href="../io/trait.Write.html"><code>Write</code></a>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::{<span class="ident">Command</span>, <span class="ident">Stdio</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Write</span>;

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">&quot;/bin/cat&quot;</span>)
    .<span class="ident">stdin</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>())
    .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>())
    .<span class="ident">spawn</span>()
    .<span class="ident">expect</span>(<span class="string">&quot;failed to execute child&quot;</span>);

{
    <span class="comment">// limited borrow of stdin</span>
    <span class="kw">let</span> <span class="ident">stdin</span> <span class="op">=</span> <span class="ident">child</span>.<span class="ident">stdin</span>.<span class="ident">as_mut</span>().<span class="ident">expect</span>(<span class="string">&quot;failed to get stdin&quot;</span>);
    <span class="ident">stdin</span>.<span class="ident">write_all</span>(<span class="string">b&quot;test&quot;</span>).<span class="ident">expect</span>(<span class="string">&quot;failed to write to stdin&quot;</span>);
}

<span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">child</span>
    .<span class="ident">wait_with_output</span>()
    .<span class="ident">expect</span>(<span class="string">&quot;failed to wait on child&quot;</span>);

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b&quot;test&quot;</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3A%7BCommand%2C%20Stdio%7D%3B%0Ause%20std%3A%3Aio%3A%3AWrite%3B%0A%0Alet%20mut%20child%20%3D%20Command%3A%3Anew(%22%2Fbin%2Fcat%22)%0A%20%20%20%20.stdin(Stdio%3A%3Apiped())%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22failed%20to%20execute%20child%22)%3B%0A%0A%7B%0A%20%20%20%20%2F%2F%20limited%20borrow%20of%20stdin%0A%20%20%20%20let%20stdin%20%3D%20child.stdin.as_mut().expect(%22failed%20to%20get%20stdin%22)%3B%0A%20%20%20%20stdin.write_all(b%22test%22).expect(%22failed%20to%20write%20to%20stdin%22)%3B%0A%7D%0A%0Alet%20output%20%3D%20child%0A%20%20%20%20.wait_with_output()%0A%20%20%20%20.expect(%22failed%20to%20wait%20on%20child%22)%3B%0A%0Aassert_eq!(b%22test%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Child.html"
                                  title='struct std::process::Child'>Child</a></td>
                           <td class='docblock-short'>
                                <p>Representation of a running or exited child process.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.ChildStderr.html"
                                  title='struct std::process::ChildStderr'>ChildStderr</a></td>
                           <td class='docblock-short'>
                                <p>A handle to a child process's stderr.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.ChildStdin.html"
                                  title='struct std::process::ChildStdin'>ChildStdin</a></td>
                           <td class='docblock-short'>
                                <p>A handle to a child process's standard input (stdin).</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.ChildStdout.html"
                                  title='struct std::process::ChildStdout'>ChildStdout</a></td>
                           <td class='docblock-short'>
                                <p>A handle to a child process's standard output (stdout).</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Command.html"
                                  title='struct std::process::Command'>Command</a></td>
                           <td class='docblock-short'>
                                <p>A process builder, providing fine-grained control
over how a new process should be spawned.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.ExitStatus.html"
                                  title='struct std::process::ExitStatus'>ExitStatus</a></td>
                           <td class='docblock-short'>
                                <p>Describes the result of a process after it has terminated.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Output.html"
                                  title='struct std::process::Output'>Output</a></td>
                           <td class='docblock-short'>
                                <p>The output of a finished process.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Stdio.html"
                                  title='struct std::process::Stdio'>Stdio</a></td>
                           <td class='docblock-short'>
                                <p>Describes what to do with a standard I/O stream for a child process when
passed to the <a href="struct.Command.html#method.stdin"><code>stdin</code></a>, <a href="struct.Command.html#method.stdout"><code>stdout</code></a>, and <a href="struct.Command.html#method.stderr"><code>stderr</code></a> methods of <a href="struct.Command.html"><code>Command</code></a>.</p>

                           </td>
                       </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.abort.html"
                                  title='fn std::process::abort'>abort</a></td>
                           <td class='docblock-short'>
                                <p>Terminates the process in an abnormal fashion.</p>

                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="fn" href="fn.exit.html"
                                  title='fn std::process::exit'>exit</a></td>
                           <td class='docblock-short'>
                                <p>Terminates the current process with the specified exit code.</p>

                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="fn" href="fn.id.html"
                                  title='fn std::process::id'>id</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] <p>Returns the OS-assigned process identifier associated with this process.</p>

                           </td>
                       </tr></table></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <aside id="help" class="hidden">
        <div>
            <h1 class="hidden">Help</h1>

            <div class="shortcuts">
                <h2>Keyboard Shortcuts</h2>

                <dl>
                    <dt><kbd>?</kbd></dt>
                    <dd>Show this help dialog</dd>
                    <dt><kbd>S</kbd></dt>
                    <dd>Focus the search field</dd>
                    <dt><kbd>↑</kbd></dt>
                    <dd>Move up in search results</dd>
                    <dt><kbd>↓</kbd></dt>
                    <dd>Move down in search results</dd>
                    <dt><kbd>↹</kbd></dt>
                    <dd>Switch tab</dd>
                    <dt><kbd>&#9166;</kbd></dt>
                    <dd>Go to active search result</dd>
                    <dt><kbd>+</kbd></dt>
                    <dd>Expand all sections</dd>
                    <dt><kbd>-</kbd></dt>
                    <dd>Collapse all sections</dd>
                </dl>
            </div>

            <div class="infos">
                <h2>Search Tricks</h2>

                <p>
                    Prefix searches with a type followed by a colon (e.g.
                    <code>fn:</code>) to restrict the search to a given type.
                </p>

                <p>
                    Accepted types are: <code>fn</code>, <code>mod</code>,
                    <code>struct</code>, <code>enum</code>,
                    <code>trait</code>, <code>type</code>, <code>macro</code>,
                    and <code>const</code>.
                </p>

                <p>
                    Search functions by type signature (e.g.
                    <code>vec -> usize</code> or <code>* -> vec</code>)
                </p>
            </div>
        </div>
    </aside>

    

    <script>
        window.rootPath = "../../";
        window.currentCrate = "std";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>