]> git.lizzy.rs Git - rust.git/blobdiff - src/doc/intro.md
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[rust.git] / src / doc / intro.md
index 886528e867216b6bfb66d5872eab576ab3009107..128d5c9d320595230ffa1d63caf406ef3fc10295 100644 (file)
@@ -36,8 +36,8 @@ int add_one(void)
 }
 ```
 
-**Note: obviously this is very simple and non-idiomatic C++.
-You wouldn't write it in practice; it is for illustrative purposes.**
+**Note: The above C++ code is deliberately simple and non-idiomatic for the purpose
+of demonstration. It is not representative of production-quality C++ code.**
 
 This function allocates an integer on the stack,
 and stores it in a variable, `i`.
@@ -133,7 +133,7 @@ Check it out:
 ```
 
 fn dangling() -> Box<int> {
-    let i = box 1234;
+    let i = box 1234i;
     return i;
 }
 
@@ -143,8 +143,8 @@ fn add_one() -> int {
 }
 ```
 
-Now instead of a stack allocated `1234`,
-we have a heap allocated `box 1234`.
+Now instead of a stack allocated `1234i`,
+we have a heap allocated `box 1234i`.
 Whereas `&` borrows a pointer to existing memory,
 creating an owned box allocates memory on the heap and places a value in it,
 giving you the sole pointer to that memory.
@@ -152,7 +152,7 @@ You can roughly compare these two lines:
 
 ```
 // Rust
-let i = box 1234;
+let i = box 1234i;
 ```
 
 ```cpp
@@ -198,14 +198,14 @@ Typically, tasks do not share memory but instead communicate amongst each other
 
 ```
 fn main() {
-    let numbers = vec![1,2,3];
+    let numbers = vec![1i, 2i, 3i];
 
     let (tx, rx)  = channel();
     tx.send(numbers);
 
     spawn(proc() {
         let numbers = rx.recv();
-        println!("{}", *numbers.get(0));
+        println!("{}", numbers[0]);
     })
 }
 ```
@@ -237,26 +237,26 @@ try to modify the previous example to continue using the variable `numbers`:
 
 ```ignore
 fn main() {
-    let numbers = vec![1,2,3];
+    let numbers = vec![1i, 2i, 3i];
 
     let (tx, rx)  = channel();
     tx.send(numbers);
 
     spawn(proc() {
         let numbers = rx.recv();
-        println!("{}", numbers.get(0));
+        println!("{}", numbers[0]);
     });
 
     // Try to print a number from the original task
-    println!("{}", *numbers.get(0));
+    println!("{}", numbers[0]);
 }
 ```
 
-This will result an error indicating that the value is no longer in scope:
+The compiler will produce an error indicating that the value is no longer in scope:
 
 ```text
 concurrency.rs:12:20: 12:27 error: use of moved value: 'numbers'
-concurrency.rs:12     println!("{}", numbers.get(0));
+concurrency.rs:12     println!("{}", numbers[0]);
                                      ^~~~~~~
 ```
 
@@ -267,16 +267,16 @@ Let's see an example that uses the `clone` method to create copies of the data:
 
 ```
 fn main() {
-    let numbers = vec![1,2,3];
+    let numbers = vec![1i, 2i, 3i];
 
-    for num in range(0, 3) {
+    for num in range(0u, 3) {
         let (tx, rx)  = channel();
         // Use `clone` to send a *copy* of the array
         tx.send(numbers.clone());
 
         spawn(proc() {
             let numbers = rx.recv();
-            println!("{:d}", *numbers.get(num as uint));
+            println!("{:d}", numbers[num]);
         })
     }
 }
@@ -300,16 +300,16 @@ Here's some code:
 use std::sync::Arc;
 
 fn main() {
-    let numbers = vec![1,2,3];
+    let numbers = vec![1i, 2i, 3i];
     let numbers = Arc::new(numbers);
 
-    for num in range(0, 3) {
+    for num in range(0u, 3) {
         let (tx, rx)  = channel();
         tx.send(numbers.clone());
 
         spawn(proc() {
             let numbers = rx.recv();
-            println!("{:d}", *numbers.get(num as uint));
+            println!("{:d}", (*numbers)[num as uint]);
         })
     }
 }
@@ -346,10 +346,10 @@ and modify it to mutate the shared state:
 use std::sync::{Arc, Mutex};
 
 fn main() {
-    let numbers = vec![1,2,3];
+    let numbers = vec![1i, 2i, 3i];
     let numbers_lock = Arc::new(Mutex::new(numbers));
 
-    for num in range(0, 3) {
+    for num in range(0u, 3) {
         let (tx, rx)  = channel();
         tx.send(numbers_lock.clone());
 
@@ -361,10 +361,10 @@ fn main() {
 
             // This is ugly for now, but will be replaced by
             // `numbers[num as uint] += 1` in the near future.
-            // See: https://github.com/mozilla/rust/issues/6515
+            // See: https://github.com/rust-lang/rust/issues/6515
             *numbers.get_mut(num as uint) = *numbers.get_mut(num as uint) + 1;
 
-            println!("{}", *numbers.get(num as uint));
+            println!("{}", (*numbers)[num as uint]);
 
             // When `numbers` goes out of scope the lock is dropped
         })