@@ -132,24 +132,23 @@ Rust program files are, by convention, given the extension `.rs`. Say
132
132
we have a file ` hello.rs ` containing this program:
133
133
134
134
~~~~
135
- use std;
136
135
fn main(args: [str]) {
137
- std:: io::println("hello world from '" + args[0] + "'!");
136
+ io::println("hello world from '" + args[0] + "'!");
138
137
}
139
138
~~~~
140
139
141
140
If the Rust compiler was installed successfully, running `rustc
142
141
hello.rs` will produce a binary called ` hello` (or ` hello.exe`).
143
142
144
- If you modify the program to make it invalid (for example, remove the
145
- ` use std ` line ), and then compile it, you'll see an error message like
146
- this:
143
+ If you modify the program to make it invalid (for example, change the
144
+ function to an unknown name ), and then compile it, you'll see an error
145
+ message like this:
147
146
148
147
~~~~
149
148
## notrust
150
- hello.rs:2:4: 2:20 error: unresolved modulename: std
151
- hello.rs:2 std:: io::println ("hello world!");
152
- ^~~~~~~~~~~~~~~~
149
+ hello.rs:2:4: 2:16 error: unresolved name: io::print_it
150
+ hello.rs:2 io::print_it ("hello world from '" + args[0] + "' !");
151
+ ^~~~~~~~~~~~
153
152
~~~~
154
153
155
154
The Rust compiler tries to provide useful information when it runs
@@ -278,8 +277,8 @@ NOTE: The parser doesn't currently recognize non-ascii alphabetic
278
277
characters. This is a bug that will eventually be fixed.
279
278
280
279
The double-colon (` :: ` ) is used as a module separator, so
281
- ` std:: io::println` means 'the thing named ` println ` in the module
282
- named ` io ` in the module named ` std ` ' .
280
+ ` io::println ` means 'the thing named ` println ` in the module
281
+ named ` io ` .
283
282
284
283
Rust will normally emit warnings about unused variables. These can be
285
284
suppressed by using a variable name that starts with an underscore.
@@ -300,7 +299,7 @@ const repeat: uint = 5u;
300
299
fn main() {
301
300
let count = 0u;
302
301
while count < repeat {
303
- std:: io::println("Hi!");
302
+ io::println("Hi!");
304
303
count += 1u;
305
304
}
306
305
}
@@ -535,7 +534,7 @@ one is `#fmt`, a printf-style text formatting macro that is expanded
535
534
at compile time.
536
535
537
536
~~~~
538
- std:: io::println(#fmt("%s is %d", "the answer", 42));
537
+ io::println(#fmt("%s is %d", "the answer", 42));
539
538
~~~~
540
539
541
540
` #fmt ` supports most of the directives that [ printf] [ pf ] supports, but
@@ -549,7 +548,7 @@ All syntax extensions look like `#word`. Another built-in one is
549
548
compile-time.
550
549
551
550
~~~~
552
- std:: io::println(#env("PATH"));
551
+ io::println(#env("PATH"));
553
552
~~~~
554
553
# Control structures
555
554
@@ -561,11 +560,11 @@ compulsory, an optional `else` clause can be appended, and multiple
561
560
562
561
~~~~
563
562
if false {
564
- std:: io::println("that's odd");
563
+ io::println("that's odd");
565
564
} else if true {
566
- std:: io::println("right");
565
+ io::println("right");
567
566
} else {
568
- std:: io::println("neither true nor false");
567
+ io::println("neither true nor false");
569
568
}
570
569
~~~~
571
570
@@ -598,10 +597,10 @@ the value.
598
597
~~~~
599
598
# let my_number = 1;
600
599
alt my_number {
601
- 0 { std:: io::println("zero"); }
602
- 1 | 2 { std:: io::println("one or two"); }
603
- 3 to 10 { std:: io::println("three to ten"); }
604
- _ { std:: io::println("something else"); }
600
+ 0 { io::println("zero"); }
601
+ 1 | 2 { io::println("one or two"); }
602
+ 3 to 10 { io::println("three to ten"); }
603
+ _ { io::println("something else"); }
605
604
}
606
605
~~~~
607
606
@@ -675,7 +674,7 @@ let x = 5;
675
674
while true {
676
675
x += x - 3;
677
676
if x % 5 == 0 { break; }
678
- std:: io::println(int::str(x));
677
+ io::println(int::str(x));
679
678
}
680
679
~~~~
681
680
@@ -697,7 +696,7 @@ When iterating over a vector, use `for` instead.
697
696
698
697
~~~~
699
698
for elt in ["red", "green", "blue"] {
700
- std:: io::println(elt);
699
+ io::println(elt);
701
700
}
702
701
~~~~
703
702
@@ -864,7 +863,7 @@ fn mk_appender(suffix: str) -> fn@(str) -> str {
864
863
865
864
fn main() {
866
865
let shout = mk_appender("!");
867
- std:: io::println(shout("hey ho, let's go"));
866
+ io::println(shout("hey ho, let's go"));
868
867
}
869
868
~~~~
870
869
@@ -1267,7 +1266,7 @@ with square brackets (zero-based):
1267
1266
1268
1267
~~~~
1269
1268
let myvec = [true, false, true, false];
1270
- if myvec[1] { std:: io::println("boom"); }
1269
+ if myvec[1] { io::println("boom"); }
1271
1270
~~~~
1272
1271
1273
1272
By default, vectors are immutable—you can not replace their elements.
@@ -1669,7 +1668,7 @@ mod farm {
1669
1668
fn cow() -> str { "mooo" }
1670
1669
}
1671
1670
fn main() {
1672
- std:: io::println(farm::chicken());
1671
+ io::println(farm::chicken());
1673
1672
}
1674
1673
~~~~
1675
1674
@@ -1788,7 +1787,7 @@ fn world() -> str { "world" }
1788
1787
// main.rs
1789
1788
use std;
1790
1789
use mylib;
1791
- fn main() { std:: io::println("hello " + mylib::world()); }
1790
+ fn main() { io::println("hello " + mylib::world()); }
1792
1791
~~~~
1793
1792
1794
1793
Now compile and run like this (adjust to your platform if necessary):
@@ -1810,21 +1809,21 @@ identifiers at the top of a file, module, or block.
1810
1809
1811
1810
~~~~
1812
1811
use std;
1813
- import std:: io::println;
1812
+ import io::println;
1814
1813
fn main() {
1815
1814
println("that was easy");
1816
1815
}
1817
1816
~~~~
1818
1817
1819
1818
It is also possible to import just the name of a module (`import
1820
- std::io ;` , then use ` io::println `), to import all identifiers exported
1821
- by a given module (` import std:: io::* ` ), or to import a specific set
1819
+ std::list ;` , then use ` list::find `), to import all identifiers exported
1820
+ by a given module (` import io::* ` ), or to import a specific set
1822
1821
of identifiers (` import math::{min, max, pi} ` ).
1823
1822
1824
1823
You can rename an identifier when importing using the ` = ` operator:
1825
1824
1826
1825
~~~~
1827
- import prnt = std:: io::println;
1826
+ import prnt = io::println;
1828
1827
~~~~
1829
1828
1830
1829
## Exporting
@@ -2158,7 +2157,7 @@ fn sha1(data: str) -> str unsafe {
2158
2157
}
2159
2158
2160
2159
fn main(args: [str]) {
2161
- std:: io::println(sha1(args[1]));
2160
+ io::println(sha1(args[1]));
2162
2161
}
2163
2162
~~~~
2164
2163
@@ -2376,8 +2375,8 @@ module `task`. Let's begin with the simplest one, `task::spawn()`:
2376
2375
~~~~
2377
2376
let some_value = 22;
2378
2377
task::spawn {||
2379
- std:: io::println("This executes in the child task.");
2380
- std:: io::println(#fmt("%d", some_value));
2378
+ io::println("This executes in the child task.");
2379
+ io::println(#fmt("%d", some_value));
2381
2380
}
2382
2381
~~~~
2383
2382
0 commit comments