index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `num_bigint` crate.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, num_bigint">
  9. <title>num_bigint - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
  14. <script src="../storage.js"></script>
  15. </head>
  16. <body class="rustdoc mod">
  17. <!--[if lte IE 8]>
  18. <div class="warning">
  19. This old browser is unsupported and will most likely display funky
  20. things.
  21. </div>
  22. <![endif]-->
  23. <nav class="sidebar">
  24. <div class="sidebar-menu">&#9776;</div>
  25. <p class='location'>Crate num_bigint</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'num_bigint', ty: 'mod', relpath: '../'};</script></div>
  26. </nav>
  27. <div class="theme-picker">
  28. <button id="theme-picker" aria-label="Pick another theme!">
  29. <img src="../brush.svg" width="18" alt="Pick another theme!">
  30. </button>
  31. <div id="theme-choices"></div>
  32. </div>
  33. <script src="../theme.js"></script>
  34. <nav class="sub">
  35. <form class="search-form js-only">
  36. <div class="search-container">
  37. <input class="search-input" name="search"
  38. autocomplete="off"
  39. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  40. type="search">
  41. </div>
  42. </form>
  43. </nav>
  44. <section id='main' class="content">
  45. <h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>num_bigint</a></span><span class='out-of-band'><span id='render-detail'>
  46. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  47. [<span class='inner'>&#x2212;</span>]
  48. </a>
  49. </span><a class='srclink' href='../src/num_bigint/lib.rs.html#11-156' title='goto source code'>[src]</a></span></h1>
  50. <div class='docblock'><p>A Big integer (signed version: <code>BigInt</code>, unsigned version: <code>BigUint</code>).</p>
  51. <p>A <code>BigUint</code> is represented as a vector of <code>BigDigit</code>s.
  52. A <code>BigInt</code> is a combination of <code>BigUint</code> and <code>Sign</code>.</p>
  53. <p>Common numerical operations are overloaded, so we can treat them
  54. the same way we treat other numbers.</p>
  55. <h2 id="example" class="section-header"><a href="#example">Example</a></h2>
  56. <pre class="rust rust-example-rendered">
  57. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num_bigint</span>;
  58. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num_traits</span>;
  59. <span class="kw">use</span> <span class="ident">num_bigint</span>::<span class="ident">BigUint</span>;
  60. <span class="kw">use</span> <span class="ident">num_traits</span>::{<span class="ident">Zero</span>, <span class="ident">One</span>};
  61. <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">mem</span>::<span class="ident">replace</span>;
  62. <span class="comment">// Calculate large fibonacci numbers.</span>
  63. <span class="kw">fn</span> <span class="ident">fib</span>(<span class="ident">n</span>: <span class="ident">usize</span>) <span class="op">-&gt;</span> <span class="ident">BigUint</span> {
  64. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">f0</span>: <span class="ident">BigUint</span> <span class="op">=</span> <span class="ident">Zero</span>::<span class="ident">zero</span>();
  65. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">f1</span>: <span class="ident">BigUint</span> <span class="op">=</span> <span class="ident">One</span>::<span class="ident">one</span>();
  66. <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">n</span> {
  67. <span class="kw">let</span> <span class="ident">f2</span> <span class="op">=</span> <span class="ident">f0</span> <span class="op">+</span> <span class="kw-2">&amp;</span><span class="ident">f1</span>;
  68. <span class="comment">// This is a low cost way of swapping f0 with f1 and f1 with f2.</span>
  69. <span class="ident">f0</span> <span class="op">=</span> <span class="ident">replace</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">f1</span>, <span class="ident">f2</span>);
  70. }
  71. <span class="ident">f0</span>
  72. }
  73. <span class="comment">// This is a very large number.</span>
  74. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;fib(1000) = {}&quot;</span>, <span class="ident">fib</span>(<span class="number">1000</span>));</pre>
  75. <p>It's easy to generate large random numbers:</p>
  76. <pre class="rust rust-example-rendered">
  77. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">rand</span>;
  78. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num_bigint</span> <span class="kw">as</span> <span class="ident">bigint</span>;
  79. <span class="kw">use</span> <span class="ident">bigint</span>::{<span class="ident">ToBigInt</span>, <span class="ident">RandBigInt</span>};
  80. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
  81. <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen_bigint</span>(<span class="number">1000</span>);
  82. <span class="kw">let</span> <span class="ident">low</span> <span class="op">=</span> <span class="op">-</span><span class="number">10000</span>.<span class="ident">to_bigint</span>().<span class="ident">unwrap</span>();
  83. <span class="kw">let</span> <span class="ident">high</span> <span class="op">=</span> <span class="number">10000</span>.<span class="ident">to_bigint</span>().<span class="ident">unwrap</span>();
  84. <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen_bigint_range</span>(<span class="kw-2">&amp;</span><span class="ident">low</span>, <span class="kw-2">&amp;</span><span class="ident">high</span>);
  85. <span class="comment">// Probably an even larger number.</span>
  86. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">a</span> <span class="op">*</span> <span class="ident">b</span>);
  87. </pre>
  88. <h2 id="compatibility" class="section-header"><a href="#compatibility">Compatibility</a></h2>
  89. <p>The <code>num-bigint</code> crate is tested for rustc 1.8 and greater.</p>
  90. </div><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
  91. <table>
  92. <tr class=' module-item'>
  93. <td><a class="mod" href="big_digit/index.html"
  94. title='mod num_bigint::big_digit'>big_digit</a></td>
  95. <td class='docblock-short'>
  96. </td>
  97. </tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  98. <table>
  99. <tr class=' module-item'>
  100. <td><a class="struct" href="struct.BigInt.html"
  101. title='struct num_bigint::BigInt'>BigInt</a></td>
  102. <td class='docblock-short'>
  103. <p>A big signed integer type.</p>
  104. </td>
  105. </tr>
  106. <tr class=' module-item'>
  107. <td><a class="struct" href="struct.BigUint.html"
  108. title='struct num_bigint::BigUint'>BigUint</a></td>
  109. <td class='docblock-short'>
  110. <p>A big unsigned integer type.</p>
  111. </td>
  112. </tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
  113. <table>
  114. <tr class=' module-item'>
  115. <td><a class="enum" href="enum.ParseBigIntError.html"
  116. title='enum num_bigint::ParseBigIntError'>ParseBigIntError</a></td>
  117. <td class='docblock-short'>
  118. </td>
  119. </tr>
  120. <tr class=' module-item'>
  121. <td><a class="enum" href="enum.Sign.html"
  122. title='enum num_bigint::Sign'>Sign</a></td>
  123. <td class='docblock-short'>
  124. <p>A Sign is a <code>BigInt</code>'s composing element.</p>
  125. </td>
  126. </tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
  127. <table>
  128. <tr class=' module-item'>
  129. <td><a class="constant" href="constant.ZERO_BIG_DIGIT.html"
  130. title='constant num_bigint::ZERO_BIG_DIGIT'>ZERO_BIG_DIGIT</a></td>
  131. <td class='docblock-short'>
  132. </td>
  133. </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
  134. <table>
  135. <tr class=' module-item'>
  136. <td><a class="trait" href="trait.RandBigInt.html"
  137. title='trait num_bigint::RandBigInt'>RandBigInt</a></td>
  138. <td class='docblock-short'>
  139. </td>
  140. </tr>
  141. <tr class=' module-item'>
  142. <td><a class="trait" href="trait.ToBigInt.html"
  143. title='trait num_bigint::ToBigInt'>ToBigInt</a></td>
  144. <td class='docblock-short'>
  145. <p>A generic trait for converting a value to a <code>BigInt</code>.</p>
  146. </td>
  147. </tr>
  148. <tr class=' module-item'>
  149. <td><a class="trait" href="trait.ToBigUint.html"
  150. title='trait num_bigint::ToBigUint'>ToBigUint</a></td>
  151. <td class='docblock-short'>
  152. <p>A generic trait for converting a value to a <code>BigUint</code>.</p>
  153. </td>
  154. </tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
  155. <table>
  156. <tr class=' module-item'>
  157. <td><a class="type" href="type.BigDigit.html"
  158. title='type num_bigint::BigDigit'>BigDigit</a></td>
  159. <td class='docblock-short'>
  160. <p>A <code>BigDigit</code> is a <code>BigUint</code>'s composing element.</p>
  161. </td>
  162. </tr>
  163. <tr class=' module-item'>
  164. <td><a class="type" href="type.DoubleBigDigit.html"
  165. title='type num_bigint::DoubleBigDigit'>DoubleBigDigit</a></td>
  166. <td class='docblock-short'>
  167. <p>A <code>DoubleBigDigit</code> is the internal type used to do the computations. Its
  168. size is the double of the size of <code>BigDigit</code>.</p>
  169. </td>
  170. </tr></table></section>
  171. <section id='search' class="content hidden"></section>
  172. <section class="footer"></section>
  173. <aside id="help" class="hidden">
  174. <div>
  175. <h1 class="hidden">Help</h1>
  176. <div class="shortcuts">
  177. <h2>Keyboard Shortcuts</h2>
  178. <dl>
  179. <dt><kbd>?</kbd></dt>
  180. <dd>Show this help dialog</dd>
  181. <dt><kbd>S</kbd></dt>
  182. <dd>Focus the search field</dd>
  183. <dt><kbd>↑</kbd></dt>
  184. <dd>Move up in search results</dd>
  185. <dt><kbd>↓</kbd></dt>
  186. <dd>Move down in search results</dd>
  187. <dt><kbd>↹</kbd></dt>
  188. <dd>Switch tab</dd>
  189. <dt><kbd>&#9166;</kbd></dt>
  190. <dd>Go to active search result</dd>
  191. <dt><kbd>+</kbd></dt>
  192. <dd>Expand all sections</dd>
  193. <dt><kbd>-</kbd></dt>
  194. <dd>Collapse all sections</dd>
  195. </dl>
  196. </div>
  197. <div class="infos">
  198. <h2>Search Tricks</h2>
  199. <p>
  200. Prefix searches with a type followed by a colon (e.g.
  201. <code>fn:</code>) to restrict the search to a given type.
  202. </p>
  203. <p>
  204. Accepted types are: <code>fn</code>, <code>mod</code>,
  205. <code>struct</code>, <code>enum</code>,
  206. <code>trait</code>, <code>type</code>, <code>macro</code>,
  207. and <code>const</code>.
  208. </p>
  209. <p>
  210. Search functions by type signature (e.g.
  211. <code>vec -> usize</code> or <code>* -> vec</code>)
  212. </p>
  213. </div>
  214. </div>
  215. </aside>
  216. <script>
  217. window.rootPath = "../";
  218. window.currentCrate = "num_bigint";
  219. </script>
  220. <script src="../main.js"></script>
  221. <script defer src="../search-index.js"></script>
  222. </body>
  223. </html>