@@ -262,7 +262,7 @@ write function, variable, and module names with lowercase letters, using
262262underscores where they help readability, while writing types in camel case.
263263
264264~~~
265- let my_variable = 100 ;
265+ let my_variable = 100i ;
266266type MyType = int; // primitive types are _not_ camel case
267267~~~
268268
@@ -276,7 +276,7 @@ write a piece of code like this:
276276
277277~~~~
278278# let item = "salad";
279- let price;
279+ let price: f64 ;
280280if item == "salad" {
281281 price = 3.50;
282282} else if item == "muffin" {
@@ -290,7 +290,7 @@ But, in Rust, you don't have to repeat the name `price`:
290290
291291~~~~
292292# let item = "salad";
293- let price =
293+ let price: f64 =
294294 if item == "salad" {
295295 3.50
296296 } else if item == "muffin" {
@@ -337,11 +337,10 @@ suffix that can be used to indicate the type of a literal: `i` for `int`,
337337In the absence of an integer literal suffix, Rust will infer the
338338integer type based on type annotations and function signatures in the
339339surrounding program. In the absence of any type information at all,
340- Rust will assume that an unsuffixed integer literal has type
341- ` int ` .
340+ Rust will report an error and request that the type be specified explicitly.
342341
343342~~~~
344- let a = 1; // `a` is an `int`
343+ let a: int = 1; // `a` is an `int`
345344let b = 10i; // `b` is an `int`, due to the `i` suffix
346345let c = 100u; // `c` is a `uint`
347346let d = 1000i32; // `d` is an `i32`
@@ -475,7 +474,7 @@ against each pattern in order until one matches. The matching pattern
475474executes its corresponding arm.
476475
477476~~~~
478- let my_number = 1 ;
477+ let my_number = 1i ;
479478match my_number {
480479 0 => println!("zero"),
481480 1 | 2 => println!("one or two"),
@@ -501,7 +500,7 @@ matches any single value. (`..`) is a different wildcard that can match
501500one or more fields in an ` enum ` variant.
502501
503502~~~
504- # let my_number = 1 ;
503+ # let my_number = 1i ;
505504match my_number {
506505 0 => { println!("zero") }
507506 _ => { println!("something else") }
@@ -584,7 +583,7 @@ keyword `break` aborts the loop, and `continue` aborts the current
584583iteration and continues with the next.
585584
586585~~~~
587- let mut cake_amount = 8 ;
586+ let mut cake_amount = 8i ;
588587while cake_amount > 0 {
589588 cake_amount -= 1;
590589}
@@ -944,7 +943,7 @@ The `box` operator performs memory allocation on the heap:
944943~~~~
945944{
946945 // an integer allocated on the heap
947- let y = box 10 ;
946+ let y = box 10i ;
948947}
949948// the destructor frees the heap memory as soon as `y` goes out of scope
950949~~~~
@@ -1165,7 +1164,7 @@ let z = x;
11651164The mutability of a value may be changed by moving it to a new owner:
11661165
11671166~~~~
1168- let r = box 13 ;
1167+ let r = box 13i ;
11691168let mut s = r; // box becomes mutable
11701169*s += 1;
11711170let t = s; // box becomes immutable
@@ -1285,9 +1284,9 @@ Using the generic `List<T>` works much like before, thanks to type inference:
12851284# Cons(value, box xs)
12861285# }
12871286let mut xs = Nil; // Unknown type! This is a `List<T>`, but `T` can be anything.
1288- xs = prepend(xs, 10 ); // Here the compiler infers `xs`'s type as `List<int>`.
1289- xs = prepend(xs, 15 );
1290- xs = prepend(xs, 20 );
1287+ xs = prepend(xs, 10i ); // Here the compiler infers `xs`'s type as `List<int>`.
1288+ xs = prepend(xs, 15i );
1289+ xs = prepend(xs, 20i );
12911290~~~
12921291
12931292The code sample above demonstrates type inference making most type annotations optional. It is
@@ -1410,12 +1409,12 @@ Beyond the properties granted by the size, an owned box behaves as a regular
14101409value by inheriting the mutability and lifetime of the owner:
14111410
14121411~~~~
1413- let x = 5 ; // immutable
1414- let mut y = 5 ; // mutable
1412+ let x = 5i ; // immutable
1413+ let mut y = 5i ; // mutable
14151414y += 2;
14161415
1417- let x = box 5 ; // immutable
1418- let mut y = box 5 ; // mutable
1416+ let x = box 5i ; // immutable
1417+ let mut y = box 5i ; // mutable
14191418*y += 2; // the `*` operator is needed to access the contained value
14201419~~~~
14211420
@@ -1507,7 +1506,7 @@ freezing enforced statically at compile-time. An example of a non-`Freeze` type
15071506is [ ` RefCell<T> ` ] [ refcell ] .
15081507
15091508~~~~
1510- let mut x = 5 ;
1509+ let mut x = 5i ;
15111510{
15121511 let y = &x; // `x` is now frozen. It cannot be modified or re-assigned.
15131512}
@@ -1523,8 +1522,8 @@ Rust uses the unary star operator (`*`) to access the contents of a
15231522box or pointer, similarly to C.
15241523
15251524~~~
1526- let owned = box 10 ;
1527- let borrowed = &20 ;
1525+ let owned = box 10i ;
1526+ let borrowed = &20i ;
15281527
15291528let sum = *owned + *borrowed;
15301529~~~
@@ -1534,9 +1533,9 @@ assignments. Such an assignment modifies the value that the pointer
15341533points to.
15351534
15361535~~~
1537- let mut owned = box 10 ;
1536+ let mut owned = box 10i ;
15381537
1539- let mut value = 20 ;
1538+ let mut value = 20i ;
15401539let borrowed = &mut value;
15411540
15421541*owned = *borrowed + 100;
@@ -1654,12 +1653,12 @@ Unicode code points, so they cannot be freely mutated without the ability to
16541653alter the length.
16551654
16561655~~~
1657- let mut xs = [1, 2, 3 ];
1656+ let mut xs = [1i, 2i, 3i ];
16581657let view = xs.mut_slice(0, 2);
16591658view[0] = 5;
16601659
16611660// The type of a mutable slice is written as `&mut [T]`
1662- let ys: &mut [int] = &mut [1, 2, 3 ];
1661+ let ys: &mut [int] = &mut [1i, 2i, 3i ];
16631662~~~
16641663
16651664Square brackets denote indexing into a slice or fixed-size vector:
0 commit comments