<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="API documentation for the Rust `FromPrimitive` trait in crate `num_traits`."><metaname="keywords"content="rust, rustlang, rust-lang, FromPrimitive"><title>num_traits::FromPrimitive - Rust</title><linkrel="stylesheet"type="text/css"href="../normalize.css"><linkrel="stylesheet"type="text/css"href="../rustdoc.css"id="mainThemeStyle"><linkrel="stylesheet"type="text/css"href="../dark.css"><linkrel="stylesheet"type="text/css"href="../light.css"id="themeStyle"><scriptsrc="../storage.js"></script><noscript><linkrel="stylesheet"href="../noscript.css"></noscript><linkrel="shortcut icon"href="../favicon.ico"><styletype="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><bodyclass="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="sidebar"><divclass="sidebar-menu">☰</div><ahref='../num_traits/index.html'><divclass='logo-container'><imgsrc='../rust-logo.png'alt='logo'></div></a><pclass='location'>Trait FromPrimitive</p><divclass="sidebar-elems"><divclass="block items"><aclass="sidebar-title"href="#required-methods">Required Methods</a><divclass="sidebar-links"><ahref="#tymethod.from_i64">from_i64</a><ahref="#tymethod.from_u64">from_u64</a></div><aclass="sidebar-title"href="#provided-methods">Provided Methods</a><divclass="sidebar-links"><ahref="#method.from_f32">from_f32</a><ahref="#method.from_f64">from_f64</a><ahref="#method.from_i128">from_i128</a><ahref="#method.from_i16">from_i16</a><ahref="#method.from_i32">from_i32</a><ahref="#method.from_i8">from_i8</a><ahref="#method.from_isize">from_isize</a><ahref="#method.from_u128">from_u128</a><ahref="#method.from_u16">from_u16</a><ahref="#method.from_u32">from_u32</a><ahref="#method.from_u8">from_u8</a><ahref="#method.from_usize">from_usize</a></div><aclass="sidebar-title"href="#foreign-impls">Implementations on Foreign Types</a><divclass="sidebar-links"><ahref="#impl-FromPrimitive-for-Wrapping%3CT%3E">Wrapping<T></a><ahref="#impl-FromPrimitive-for-f32">f32</a><ahref="#impl-FromPrimitive-for-f64">f64</a><ahref="#impl-FromPrimitive-for-i128">i128</a><ahref="#impl-FromPrimitive-for-i16">i16</a><ahref="#impl-FromPrimitive-for-i32">i32</a><ahref="#impl-FromPrimitive-for-i64">i64</a><ahref="#impl-FromPrimitive-for-i8">i8</a><ahref="#impl-FromPrimitive-for-isize">isize</a><ahref="#impl-FromPrimitive-for-u128">u128</a><ahref="#impl-FromPrimitive-for-u16">u16</a><ahref="#impl-FromPrimitive-for-u32">u32</a><ahref="#impl-FromPrimitive-for-u64">u64</a><ahref="#impl-FromPrimitive-for-u8">u8</a><ahref="#impl-FromPrimitive-for-usize">usize</a></div><aclass="sidebar-title"href="#implementors">Implementors</a></div><pclass='location'><ahref='index.html'>num_traits</a></p><script>window.sidebarCurrent={name:'FromPrimitive',ty:'trait',relpath:''};</script><scriptdefersrc="sidebar-items.js"></script></div></nav><divclass="theme-picker"><buttonid="theme-picker"aria-label="Pick another theme!"><imgsrc="../brush.svg"width="18"alt="Pick another theme!"></button><divid="theme-choices"></div></div><scriptsrc="../theme.js"></script><navclass="sub"><formclass="search-form js-only"><divclass="search-container"><div><selectid="crate-search"><optionvalue="All crates">All crates</option></select><inputclass="search-input"name="search"autocomplete="off"spellcheck="false"placeholder="Click or press ‘S’ to search, ‘?’ for more options…"type="search"></div><aid="settings-menu"href="../settings.html"><imgsrc="../wheel.svg"width="18"alt="Change settings"></a></div></form></nav><sectionid="main"class="content"><h1class='fqn'><spanclass='out-of-band'><spanid='render-detail'><aid="toggle-all-docs"href="javascript:void(0)"title="collapse all docs">[<span
}</pre></div><divclass='docblock'><p>A generic trait for converting a number to a value.</p>
</div>
<h2id='required-methods'class='small-section-header'>Required methods<ahref='#required-methods'class='anchor'></a></h2><divclass='methods'><h3id='tymethod.from_i64'class='method'><codeid='from_i64.v'>fn <ahref='#tymethod.from_i64'class='fnname'>from_i64</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>i64</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='tymethod.from_u64'class='method'><codeid='from_u64.v'>fn <ahref='#tymethod.from_u64'class='fnname'>from_u64</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>u64</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
<h2id='provided-methods'class='small-section-header'>Provided methods<ahref='#provided-methods'class='anchor'></a></h2><divclass='methods'><h3id='method.from_isize'class='method'><codeid='from_isize.v'>fn <ahref='#method.from_isize'class='fnname'>from_isize</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.isize.html">isize</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>isize</code> to return an optional value of this type. If the
value cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_i8'class='method'><codeid='from_i8.v'>fn <ahref='#method.from_i8'class='fnname'>from_i8</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>i8</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_i16'class='method'><codeid='from_i16.v'>fn <ahref='#method.from_i16'class='fnname'>from_i16</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>i16</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_i32'class='method'><codeid='from_i32.v'>fn <ahref='#method.from_i32'class='fnname'>from_i32</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>i32</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_i128'class='method'><codeid='from_i128.v'>fn <ahref='#method.from_i128'class='fnname'>from_i128</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.i128.html">i128</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>i128</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
<p>This method is only available with feature <code>i128</code> enabled on Rust >= 1.26.</p>
<p>The default implementation converts through <code>from_i64()</code>. Types implementing
this trait should override this method if they can represent a greater range.</p>
</div><h3id='method.from_usize'class='method'><codeid='from_usize.v'>fn <ahref='#method.from_usize'class='fnname'>from_usize</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert a <code>usize</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_u8'class='method'><codeid='from_u8.v'>fn <ahref='#method.from_u8'class='fnname'>from_u8</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>u8</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_u16'class='method'><codeid='from_u16.v'>fn <ahref='#method.from_u16'class='fnname'>from_u16</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u16.html">u16</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>u16</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_u32'class='method'><codeid='from_u32.v'>fn <ahref='#method.from_u32'class='fnname'>from_u32</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>u32</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_u128'class='method'><codeid='from_u128.v'>fn <ahref='#method.from_u128'class='fnname'>from_u128</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u128.html">u128</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert an <code>u128</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
<p>This method is only available with feature <code>i128</code> enabled on Rust >= 1.26.</p>
<p>The default implementation converts through <code>from_u64()</code>. Types implementing
this trait should override this method if they can represent a greater range.</p>
</div><h3id='method.from_f32'class='method'><codeid='from_f32.v'>fn <ahref='#method.from_f32'class='fnname'>from_f32</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert a <code>f32</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</div><h3id='method.from_f64'class='method'><codeid='from_f64.v'>fn <ahref='#method.from_f64'class='fnname'>from_f64</a>(n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -><aclass="enum"href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html"title="enum core::option::Option">Option</a><Self></code></h3><divclass='docblock'><p>Convert a <code>f64</code> to return an optional value of this type. If the
type cannot be represented by this value, then <code>None</code> is returned.</p>
</script></section><sectionid="search"class="content hidden"></section><sectionclass="footer"></section><asideid="help"class="hidden"><div><h1class="hidden">Help</h1><divclass="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>⏎</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><divclass="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><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath="../";window.currentCrate="num_traits";</script><scriptsrc="../aliases.js"></script><scriptsrc="../main.js"></script><scriptdefersrc="../search-index.js"></script></body></html>