Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 878cdd00a13d17a73c6619a777ef5d74 > files > 721

rust-doc-1.19.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 `any` mod in crate `core`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, any">

    <title>core::any - Rust</title>

    <link rel="stylesheet" type="text/css" href="../../normalize.css">
    <link rel="stylesheet" type="text/css" href="../../rustdoc.css">
    <link rel="stylesheet" type="text/css" href="../../main.css">
    

    <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">
        <a href='../../core/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 any</p><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'><a href='../index.html'>core</a></p><script>window.sidebarCurrent = {name: 'any', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script>
    </nav>

    <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'>core</a>::<wbr><a class="mod" href=''>any</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/core/any.rs.html#11-375' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>This module implements the <code>Any</code> trait, which enables dynamic typing
of any <code>&#39;static</code> type through runtime reflection.</p>

<p><code>Any</code> itself can be used to get a <code>TypeId</code>, and has more features when used
as a trait object. As <code>&amp;Any</code> (a borrowed trait object), it has the <code>is</code> and
<code>downcast_ref</code> methods, to test if the contained value is of a given type,
and to get a reference to the inner value as a type. As <code>&amp;mut Any</code>, there
is also the <code>downcast_mut</code> method, for getting a mutable reference to the
inner value. <code>Box&lt;Any&gt;</code> adds the <code>downcast</code> method, which attempts to
convert to a <code>Box&lt;T&gt;</code>. See the <a href="../../std/boxed/struct.Box.html"><code>Box</code></a> documentation for the full details.</p>

<p>Note that &amp;Any is limited to testing whether a value is of a specified
concrete type, and cannot be used to test whether a type implements a trait.</p>

<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<p>Consider a situation where we want to log out a value passed to a function.
We know the value we&#39;re working on implements Debug, but we don&#39;t know its
concrete type.  We want to give special treatment to certain types: in this
case printing out the length of String values prior to their value.
We don&#39;t know the concrete type of our value at compile time, so we need to
use runtime reflection instead.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fmt</span>::<span class="ident">Debug</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">any</span>::<span class="ident">Any</span>;

<span class="comment">// Logger function for any type that implements Debug.</span>
<span class="kw">fn</span> <span class="ident">log</span><span class="op">&lt;</span><span class="ident">T</span>: <span class="ident">Any</span> <span class="op">+</span> <span class="ident">Debug</span><span class="op">&gt;</span>(<span class="ident">value</span>: <span class="kw-2">&amp;</span><span class="ident">T</span>) {
    <span class="kw">let</span> <span class="ident">value_any</span> <span class="op">=</span> <span class="ident">value</span> <span class="kw">as</span> <span class="kw-2">&amp;</span><span class="ident">Any</span>;

    <span class="comment">// try to convert our value to a String.  If successful, we want to</span>
    <span class="comment">// output the String&#39;s length as well as its value.  If not, it&#39;s a</span>
    <span class="comment">// different type: just print it out unadorned.</span>
    <span class="kw">match</span> <span class="ident">value_any</span>.<span class="ident">downcast_ref</span>::<span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>() {
        <span class="prelude-val">Some</span>(<span class="ident">as_string</span>) <span class="op">=&gt;</span> {
            <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;String ({}): {}&quot;</span>, <span class="ident">as_string</span>.<span class="ident">len</span>(), <span class="ident">as_string</span>);
        }
        <span class="prelude-val">None</span> <span class="op">=&gt;</span> {
            <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">value</span>);
        }
    }
}

<span class="comment">// This function wants to log its parameter out prior to doing work with it.</span>
<span class="kw">fn</span> <span class="ident">do_work</span><span class="op">&lt;</span><span class="ident">T</span>: <span class="ident">Any</span> <span class="op">+</span> <span class="ident">Debug</span><span class="op">&gt;</span>(<span class="ident">value</span>: <span class="kw-2">&amp;</span><span class="ident">T</span>) {
    <span class="ident">log</span>(<span class="ident">value</span>);
    <span class="comment">// ...do some other work</span>
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">my_string</span> <span class="op">=</span> <span class="string">&quot;Hello World&quot;</span>.<span class="ident">to_string</span>();
    <span class="ident">do_work</span>(<span class="kw-2">&amp;</span><span class="ident">my_string</span>);

    <span class="kw">let</span> <span class="ident">my_i8</span>: <span class="ident">i8</span> <span class="op">=</span> <span class="number">100</span>;
    <span class="ident">do_work</span>(<span class="kw-2">&amp;</span><span class="ident">my_i8</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=use%20std%3A%3Afmt%3A%3ADebug%3B%0Ause%20std%3A%3Aany%3A%3AAny%3B%0A%0A%2F%2F%20Logger%20function%20for%20any%20type%20that%20implements%20Debug.%0Afn%20log%3CT%3A%20Any%20%2B%20Debug%3E(value%3A%20%26T)%20%7B%0A%20%20%20%20let%20value_any%20%3D%20value%20as%20%26Any%3B%0A%0A%20%20%20%20%2F%2F%20try%20to%20convert%20our%20value%20to%20a%20String.%20%20If%20successful%2C%20we%20want%20to%0A%20%20%20%20%2F%2F%20output%20the%20String's%20length%20as%20well%20as%20its%20value.%20%20If%20not%2C%20it's%20a%0A%20%20%20%20%2F%2F%20different%20type%3A%20just%20print%20it%20out%20unadorned.%0A%20%20%20%20match%20value_any.downcast_ref%3A%3A%3CString%3E()%20%7B%0A%20%20%20%20%20%20%20%20Some(as_string)%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22String%20(%7B%7D)%3A%20%7B%7D%22%2C%20as_string.len()%2C%20as_string)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20None%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20value)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20This%20function%20wants%20to%20log%20its%20parameter%20out%20prior%20to%20doing%20work%20with%20it.%0Afn%20do_work%3CT%3A%20Any%20%2B%20Debug%3E(value%3A%20%26T)%20%7B%0A%20%20%20%20log(value)%3B%0A%20%20%20%20%2F%2F%20...do%20some%20other%20work%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20my_string%20%3D%20%22Hello%20World%22.to_string()%3B%0A%20%20%20%20do_work(%26my_string)%3B%0A%0A%20%20%20%20let%20my_i8%3A%20i8%20%3D%20100%3B%0A%20%20%20%20do_work(%26my_i8)%3B%0A%7D%0A">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.TypeId.html"
                                  title='struct core::any::TypeId'>TypeId</a></td>
                           <td class='docblock-short'>
                                <p>A <code>TypeId</code> represents a globally unique identifier for a type.</p>
                           </td>
                       </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="trait" href="trait.Any.html"
                                  title='trait core::any::Any'>Any</a></td>
                           <td class='docblock-short'>
                                <p>A type to emulate dynamic typing.</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>?</dt>
                    <dd>Show this help dialog</dd>
                    <dt>S</dt>
                    <dd>Focus the search field</dd>
                    <dt>&larrb;</dt>
                    <dd>Move up in search results</dd>
                    <dt>&rarrb;</dt>
                    <dd>Move down in search results</dd>
                    <dt>&#9166;</dt>
                    <dd>Go to active search result</dd>
                    <dt>+</dt>
                    <dd>Collapse/expand 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 = "core";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>