Infosec.Pub
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
amargo85 to Rust Programming@lemmy.mlEnglish · 2 years ago

RUST - Understanding integers and floats

chat-to.dev

external-link
message-square
0
link
fedilink
  • cross-posted to:
  • learn_programming@programming.dev
1
external-link

RUST - Understanding integers and floats

chat-to.dev

amargo85 to Rust Programming@lemmy.mlEnglish · 2 years ago
message-square
0
link
fedilink
  • cross-posted to:
  • learn_programming@programming.dev
Rust has signed integers (denoted by **i**) and unsigned integers (denoted by **u**) that consist of 8, 16, 32, 64, and 128 bits. The math behind binary notation is not relevant for the scope of this book. What we do need to understand, though, is the range of numbers allowed in terms of bits. Because binary is either 0 or 1, we can calculate the integer range by raising two to the power of the number of bits. For example, for 8 bits, 2 to the power of 8 equates to 256. Considering the 0, this means that an **i8** integer should have a range of 0 to 255, which can be tested by using the following code: let number: u8 = 255; Let's take a look at the following code: ```rust let number: u8 = 256; ``` It's not surprising that the preceding code gives us the following overflow error: ```rust literal `256` does not fit into the type `u8` whose range is ``0..=255 ``` What's mot expected is if we change it to a singned integer: ```rust let number: i8 = 255; ``` Here, we get the following error: ```rust literal `255` does not fit into the type `i8` whose range is `-128..=127` ``` This is because unsigned integers only house positive integers and signed integers house positive and negative integers. Since bits are memory size, the signed integer has to accommodate a range on both sides of zero, so the modulus of the signed integers is essentially half. In terms of floats, Rust accommodates **f32** and **f64** floating points, which can be both negative and positive. Declaring a floating-point variable requires the same syntax as integers: let float: f32 = 20.6; It has to be noted that we can also annotate numbers with suffixes, as shown in the following code: let x = 1u8; Here, x has a value of 1 with the type of u8. Now that we have covered floats and integers, we can use vectors and arrays to store them.
alert-triangle
You must log in or # to comment.

Rust Programming@lemmy.ml

rust@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !rust@lemmy.ml
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 2 users / day
  • 2 users / week
  • 2 users / month
  • 6 users / 6 months
  • 80 local subscribers
  • 8.14K subscribers
  • 247 Posts
  • 688 Comments
  • Modlog
  • mods:
  • nutomic@lemmy.ml
  • Joe@lemmy.ml
  • AgreeableLandscape@lemmy.ml
  • BE: 0.19.13
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org