avatarAnkit Tanna

Summary

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3354

Abstract

y</code> and <code>negative_five</code>. Rust is a bit of international friendly language so for comma separated values, you can use <code></code> for separating the digits in an integar. For example <code>one_thousand</code> is assigned a value <code>1_000</code>. Rust treats <code></code> as whitespace and ignores them. It is just a way to keep things visibly friendly and conventional for the developers developing around the world. <code>1_000</code> is same as <code>1000</code> or <code>10_00</code>. When you divide an integer with an integer then the output of it would also be an integer. so <code>exactly_three</code> which is a computation of <code>10/3</code> will give you an output of <code>3</code> and not <code>3.333333</code>. Integers cannot handle division by 0. They will <code>panic</code>. So <code>5/0</code> will result in <code>panic</code> and the program to stop then and there itself. On the contrary, if you divide a float <code>f64</code> or <code>f32</code> by <code>0</code> then the output would be <code>infinity</code> or <code>negative infinity</code> or <code>NaN</code>.</p><p id="7565">There are multiple integer sizes ranging from 8 to 128 bits. Below are the list of all the integer sizes:</p><ul><li><code>i8</code> : -128 to 127</li><li><code>i16</code> : -32,768 to 32767</li><li><code>i32</code> : −2³¹ to 2³¹-1</li><li><code>i64</code> : -2⁶³ to 2⁶³-1</li><li><code>i128</code> : -2¹²⁷ to 2¹²⁷-1</li></ul><p id="d96d">These ranges are quite huge and can handle certain use cases involving large numbers.</p><p id="3399">Theres also unsigned integers which start from <code>u</code>. Unsigned integers basically mean that their range starts from <code>0</code> and not from negative numbers. So, as compared to integers starting from <code>i</code> below is the ranges for unsigned integers:</p><ul><li><code>u8</code> : 0 to 255</li><li><code>u16</code> : 0 to 65,535</li><li><code>u32</code> : 0 to 2³²-1</li><li><code>u64</code> : 0 to 2⁶⁴-1</li><li><code>u128</code> : 0 to 2¹²⁸-1</li></ul><p id="d415">There can be couple of reasons where you may want to use an unsigned integer over a signed integer:</p><ul><li>You have some size constraint on how many bites you want to use for your storage</li><li>You don’t have negative numbers. You want to ensure that the numbers are not negative</li></ul><p id="f60b">There is also one more type <code>char</code> which is of <code>u32</code> type and is unicode validated.</p><p id="781b">The Rust compiler does not recommend using unwanted typecasting so four <code>i8</code>s don’t make a single <code>i32</code> . You can’t do inter-type multiplication of unsigned and signed integers. So you cannot multiply <code>i8</code> and <code>u8</code>.</p><p id="d780">You can typecast the numbers using <code>as</code> keyword. Take a look at below code snippet:</p><div id="fec7"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">result</span> = <span class="hljs-title function_ invoke__">multiply</span>(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, result);

}

<span class="hljs-keyword">fn</span> <span class="hljs-title func

Options

tion_">multiply</span>(x: <span class="hljs-type">i64</span>, y: <span class="hljs-type">u8</span>) <span class="hljs-punctuation">-></span> <span class="hljs-type">i64</span> { <span class="hljs-keyword">return</span> x * (y <span class="hljs-keyword">as</span> <span class="hljs-type">i64</span>); }</pre></div><h2 id="ae08">Observations:</h2><ol><li>We are using <code>as</code> keyword to convert <code>y</code> which is of type <code>u8</code> to type <code>i64</code> before multiplying it with <code>x</code> which is of type <code>i64</code>.</li><li>You could also convert <code>x</code> to <code>u8</code> before multiplying it with <code>y</code> which is of type <code>u8</code>.</li><li>It’s better to convert it to <code>i64</code> as <code>i64</code> can accommodate all the numbers that <code>u8</code> can accommodate. So in general convention, always convert to types which are superset of the other.</li></ol><h2 id="7074">Output:</h2><figure id="20cb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*tVQSSH5xAbstjahtwkCIkw.png"><figcaption></figcaption></figure><p id="a593">You can also convert the integers and unsigned integers to floats. Take a look at below code snippet:</p><div id="afc1"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">result</span> = <span class="hljs-title function_ invoke__">divide</span>(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, result);

}

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">divide</span>(x: <span class="hljs-type">i32</span>, y: <span class="hljs-type">u16</span>) <span class="hljs-punctuation">-></span> <span class="hljs-type">f64</span> { <span class="hljs-title function_ invoke__">return</span> (x <span class="hljs-keyword">as</span> <span class="hljs-type">f64</span>) / (y <span class="hljs-keyword">as</span> <span class="hljs-type">f64</span>); }</pre></div><h2 id="5824">Output:</h2><figure id="a518"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rZtmEJzQTqOVHdN991HrvQ.png"><figcaption></figcaption></figure><p id="4ee7">I hope you enjoyed this article with the details about integers/unsigned integers and type casting. Please share your feedback in the comments.</p><p id="cd7c">You can subscribe to my newsletter about <b>The Rust Programming Language <a href="https://tinyletter.com/ankittanna"></a></b><a href="https://tinyletter.com/ankittanna">here</a>. You can read about all the articles in this series <a href="https://readmedium.com/the-rust-programming-language-4b22bc717ecc">here</a>.</p><h2 id="dbae">Rustaceans 🚀</h2><p id="1fed">Thank you for being a part of the Rustaceans community! Before you go:</p><ul><li>Show your appreciation with a clap and follow the publication</li><li>Discover how you can contribute your own insights to <a href="https://readmedium.com/rustaceans-submissions-are-now-open-924e67cc595d"><b><i>Rustaceans</i></b></a>.</li><li>Connect with us: <a href="https://twitter.com/rustaceans_rs"><b><i>X</i></b></a> | <a href="https://substack.com/@weeklyrust"><b><i>Weekly Rust Newsletter</i></b></a></li></ul></article></body>

The Rust Programming Language

The Rust Programming Language — Primitives — Float sizes Integers

We have different sizes of floats and integers. f64 and f32. And as the name suggests, f64 is 64 bit memory variable so it can hold more number of digits and f32 is 32 bit memory variable so it can hold lesser number of digits. So f64 is useful where more precision is of importance in the numbers.

Why not use “f64” everywhere?

More memory used allows for more precision but also may slow down the program. Not when you have lesser number of f64 variables in your programs but when you have thousands of variables it can significantly increase the RAM consumption.

Take for example below code snippet:

fn main() {
    let x: f64 = 10.0/3.0;
    let y: f32 = 10.0/3.0;

    println!("{}", x);
    println!("{}", y);
}

Output:

Above output shows the difference in the number of decimals stored for f64 and f32 type variables.

Where there is a need of performance intensive computations, developers often tend to use f32 rather than f64 which gives them slight advantage over memory consumed and performance of the library.

Let’s have a look at some of the integers in Rust as there is slight behavioral difference when using integers.

Take for example below code snippet:

fn main() {
    let ninety = 90;
    let negative_five = -5;
    let one_thousand = 1_000;
    let exactly_three = 10 / 3;
    let this_will_panic = 5 / 0;
}

You can have positive and negative integers as seen in ninety and negative_five. Rust is a bit of international friendly language so for comma separated values, you can use _ for separating the digits in an integar. For example one_thousand is assigned a value 1_000. Rust treats _ as whitespace and ignores them. It is just a way to keep things visibly friendly and conventional for the developers developing around the world. 1_000 is same as 1000 or 10_00. When you divide an integer with an integer then the output of it would also be an integer. so exactly_three which is a computation of 10/3 will give you an output of 3 and not 3.333333. Integers cannot handle division by 0. They will panic. So 5/0 will result in panic and the program to stop then and there itself. On the contrary, if you divide a float f64 or f32 by 0 then the output would be infinity or negative infinity or NaN.

There are multiple integer sizes ranging from 8 to 128 bits. Below are the list of all the integer sizes:

  • i8 : -128 to 127
  • i16 : -32,768 to 32767
  • i32 : −2³¹ to 2³¹-1
  • i64 : -2⁶³ to 2⁶³-1
  • i128 : -2¹²⁷ to 2¹²⁷-1

These ranges are quite huge and can handle certain use cases involving large numbers.

Theres also unsigned integers which start from u. Unsigned integers basically mean that their range starts from 0 and not from negative numbers. So, as compared to integers starting from i below is the ranges for unsigned integers:

  • u8 : 0 to 255
  • u16 : 0 to 65,535
  • u32 : 0 to 2³²-1
  • u64 : 0 to 2⁶⁴-1
  • u128 : 0 to 2¹²⁸-1

There can be couple of reasons where you may want to use an unsigned integer over a signed integer:

  • You have some size constraint on how many bites you want to use for your storage
  • You don’t have negative numbers. You want to ensure that the numbers are not negative

There is also one more type char which is of u32 type and is unicode validated.

The Rust compiler does not recommend using unwanted typecasting so four i8s don’t make a single i32 . You can’t do inter-type multiplication of unsigned and signed integers. So you cannot multiply i8 and u8.

You can typecast the numbers using as keyword. Take a look at below code snippet:

fn main() {
    let result = multiply(10, 20);

    println!("{}", result);
}

fn multiply(x: i64, y: u8) -> i64 {
    return x * (y as i64);
}

Observations:

  1. We are using as keyword to convert y which is of type u8 to type i64 before multiplying it with x which is of type i64.
  2. You could also convert x to u8 before multiplying it with y which is of type u8.
  3. It’s better to convert it to i64 as i64 can accommodate all the numbers that u8 can accommodate. So in general convention, always convert to types which are superset of the other.

Output:

You can also convert the integers and unsigned integers to floats. Take a look at below code snippet:

fn main() {
    let result = divide(10, 20);

    println!("{}", result);
}

fn divide(x: i32, y: u16) -> f64 {
    return (x as f64) / (y as f64);
}

Output:

I hope you enjoyed this article with the details about integers/unsigned integers and type casting. Please share your feedback in the comments.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Rustaceans 🚀

Thank you for being a part of the Rustaceans community! Before you go:

  • Show your appreciation with a clap and follow the publication
  • Discover how you can contribute your own insights to Rustaceans.
  • Connect with us: X | Weekly Rust Newsletter
Rust
Rust Programming Language
Rustlang
Programming
Webassembly
Recommended from ReadMedium