blob: dbcf12c613c0a4613fcf692c5aae8832cbab494b [file] [log] [blame]
<!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="A Big integer (signed version: `BigInt`, unsigned version: `BigUint`)."><meta name="keywords" content="rust, rustlang, rust-lang, num_bigint"><title>num_bigint - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../num_bigint/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../num_bigint/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate num_bigint</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.2.6</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">num_bigint</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/num_bigint/lib.rs.html#11-233">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A Big integer (signed version: <code>BigInt</code>, unsigned version: <code>BigUint</code>).</p>
<p>A <code>BigUint</code> is represented as a vector of <code>BigDigit</code>s.
A <code>BigInt</code> is a combination of <code>BigUint</code> and <code>Sign</code>.</p>
<p>Common numerical operations are overloaded, so we can treat them
the same way we treat other numbers.</p>
<h3 id="example"><a href="#example">Example</a></h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">extern crate </span>num_bigint;
<span class="kw">extern crate </span>num_traits;
<span class="kw">use </span>num_bigint::BigUint;
<span class="kw">use </span>num_traits::{Zero, One};
<span class="kw">use </span>std::mem::replace;
<span class="comment">// Calculate large fibonacci numbers.
</span><span class="kw">fn </span>fib(n: usize) -&gt; BigUint {
<span class="kw">let </span><span class="kw-2">mut </span>f0: BigUint = Zero::zero();
<span class="kw">let </span><span class="kw-2">mut </span>f1: BigUint = One::one();
<span class="kw">for _ in </span><span class="number">0</span>..n {
<span class="kw">let </span>f2 = f0 + <span class="kw-2">&amp;</span>f1;
<span class="comment">// This is a low cost way of swapping f0 with f1 and f1 with f2.
</span>f0 = replace(<span class="kw-2">&amp;mut </span>f1, f2);
}
f0
}
<span class="comment">// This is a very large number.
</span><span class="macro">println!</span>(<span class="string">&quot;fib(1000) = {}&quot;</span>, fib(<span class="number">1000</span>));</code></pre></div>
<p>It’s easy to generate large random numbers:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">extern crate </span>rand;
<span class="kw">extern crate </span>num_bigint <span class="kw">as </span>bigint;
<span class="kw">use </span>bigint::{ToBigInt, RandBigInt};
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::thread_rng();
<span class="kw">let </span>a = rng.gen_bigint(<span class="number">1000</span>);
<span class="kw">let </span>low = -<span class="number">10000</span>.to_bigint().unwrap();
<span class="kw">let </span>high = <span class="number">10000</span>.to_bigint().unwrap();
<span class="kw">let </span>b = rng.gen_bigint_range(<span class="kw-2">&amp;</span>low, <span class="kw-2">&amp;</span>high);
<span class="comment">// Probably an even larger number.
</span><span class="macro">println!</span>(<span class="string">&quot;{}&quot;</span>, a * b);
</code></pre></div>
<p>See the “Features” section for instructions for enabling random number generation.</p>
<h3 id="features"><a href="#features">Features</a></h3>
<p>The <code>std</code> crate feature is mandatory and enabled by default. If you depend on
<code>num-bigint</code> with <code>default-features = false</code>, you must manually enable the
<code>std</code> feature yourself. In the future, we hope to support <code>#![no_std]</code> with
the <code>alloc</code> crate when <code>std</code> is not enabled.</p>
<p>Implementations for <code>i128</code> and <code>u128</code> are only available with Rust 1.26 and
later. The build script automatically detects this, but you can make it
mandatory by enabling the <code>i128</code> crate feature.</p>
<h4 id="random-generation"><a href="#random-generation">Random Generation</a></h4>
<p><code>num-bigint</code> supports the generation of random big integers when the <code>rand</code>
feature is enabled. To enable it include rand as</p>
<div class="example-wrap"><pre class="language-toml"><code>rand = &quot;0.5&quot;
num-bigint = { version = &quot;0.2&quot;, features = [&quot;rand&quot;] }</code></pre></div>
<p>Note that you must use the version of <code>rand</code> that <code>num-bigint</code> is compatible
with: <code>0.5</code>.</p>
<h3 id="compatibility"><a href="#compatibility">Compatibility</a></h3>
<p>The <code>num-bigint</code> crate is tested for rustc 1.15 and greater.</p>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.BigInt.html" title="num_bigint::BigInt struct">BigInt</a></div><div class="item-right docblock-short">A big signed integer type.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.BigUint.html" title="num_bigint::BigUint struct">BigUint</a></div><div class="item-right docblock-short">A big unsigned integer type.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ParseBigIntError.html" title="num_bigint::ParseBigIntError struct">ParseBigIntError</a></div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Sign.html" title="num_bigint::Sign enum">Sign</a></div><div class="item-right docblock-short">A Sign is a <code>BigInt</code>’s composing element.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ToBigInt.html" title="num_bigint::ToBigInt trait">ToBigInt</a></div><div class="item-right docblock-short">A generic trait for converting a value to a <code>BigInt</code>. This may return
<code>None</code> when converting from <code>f32</code> or <code>f64</code>, and will always succeed
when converting from any integer or unsigned primitive, or <code>BigUint</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ToBigUint.html" title="num_bigint::ToBigUint trait">ToBigUint</a></div><div class="item-right docblock-short">A generic trait for converting a value to a <code>BigUint</code>.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="num_bigint" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>