Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yay, no more int/uint! #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions primitives.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Primitive types and operators

TODO int/uint -> isuze/usize

Rust has pretty much the same arithmetic and logical operators as C++. `bool` is
the same in both languages (as are the `true` and `false` literals). Rust has
similar concepts of integers, unsigned integers, and floats. However the syntax
is a bit different. Rust uses `int` to mean an integer and `uint` to mean an
unsigned integer. These types are pointer sized. E.g., on a 32 bit system,
`uint` means a 32 bit unsigned integer. Rust also has explicitly sized types
which are `u` or `i` followed by 8, 16, 32, or 64. So, for example, `u8` is an 8
bit unsigned integer and `i32` is a 32 bit signed integer. For floats, Rust has
`f32` and `f64`.
is a bit different. Rust uses `isize` to mean a pointer-sized integer and `usize` to mean an
unsigned pointed-sized integer. As these types are pointer-sized, on a 32-bit system,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/pointed/pointer

`usize` means a 32-bit unsigned integer and on a 64-bit system it means a 64-bit integer.

However, most of the time you don't want to be working with pointer-sized integers,
so Rust also has plenty of types such as `i32`, `u8`, `f64`, etc. These take the form of
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/also has plenty of/has

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add a sentence to say why you should mostly avoid pointer-sized ints

either `i` for integers, `u` for unsized integers, or `f` for floats, followed by the
type's size. The complete list of these types looks like: `i8`, `i16`, `i32`, `i64`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/looks like/is

`u8`, `u16`, `u32`, `u64`, `f32`, `f64`.

Numeric literals can take suffixes to indicate their type (using `i` and `u`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the i and u here should be is and us. Alternatively, we could use isize and usize as the suffix and skip this clause altogether

instead of `int` and `uint`). If no suffix is given, Rust tries to infer the
type. If it can't infer, it uses `int` or `f64` (if there is a decimal point).
Examples:
instead of `isize` and `usize`). If no suffix is given, Rust tries to infer the
type. Examples:

```rust
fn main() {
let x: bool = true;
let x = 34; // type int
let x = 34u; // type uint
let x = 34is; // x: isize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised 34is still works

let x = 34us; // x: usize
let x: u8 = 34u8;
let x = 34i64;
let x = 34f32;
Expand All @@ -43,7 +43,7 @@ fn main() {
let x = 0b1100;
let x = 0o14;
let x = 0xe;
let y = 0b_1100_0011_1011_0001;
let x = 0b_1100_0011_1011_0001;
}
```

Expand All @@ -58,7 +58,7 @@ type. `as` cannot be used to convert between booleans and numeric types. E.g.,

```rust
fn main() {
let x = 34u as int; // cast unsigned int to int
let x = 34u32 as isize; // cast unsigned 32-bit int to pointer-sized int
let x = 10 as f32; // int to float
let x = 10.45f64 as i8; // float to int (loses precision)
let x = 4u8 as u64; // gains precision
Expand Down