Tie up some ends in the tutorial

This commit is contained in:
Marijn Haverbeke 2011-11-01 15:41:14 +01:00
parent a6d856c57c
commit 4fec1798d0
9 changed files with 63 additions and 11 deletions

View File

@ -105,7 +105,7 @@ and `&&` for by(-immutable)-reference. It is sometimes necessary to
override the defaults. We'll talk more about this when discussing
[generics][gens].
[gens]: FIXME
[gens]: generic.html
## Other uses of safe references

View File

@ -148,7 +148,7 @@ handle the failure, allowing the program to continue running.
to access a vector out of bounds, or running a pattern match with no
matching clauses, both result in the equivalent of a `fail`.
[tasks]: FIXME
[tasks]: task.html
## Logging

View File

@ -292,4 +292,17 @@ strings. They are always immutable.
## Resources
FIXME fill this in
Resources are data types that have a destructor associated with them.
resource file_desc(fd: int) {
close_file_desc(fd);
}
This defines a type `file_desc` and a constructor of the same name,
which takes an integer. Values of such a type can not be copied, and
when they are destroyed (by going out of scope, or, when boxed, when
their box is cleaned up), their body runs. In the example above, this
would cause the given file descriptor to be closed.
NOTE: We're considering alternative approaches for data types with
destructors. Resources might go away in the future.

View File

@ -102,3 +102,6 @@ pass to a generic higher-order function as being passed by pointer:
NOTE: This is inconvenient, and we are hoping to get rid of this
restriction in the future.
FIXME discuss kinds, when they have settled

View File

@ -35,7 +35,7 @@ It is also possible to include multiple files in a crate. For this
purpose, you create a `.rc` crate file, which references any number of
`.rs` code files. A crate file could look like this:
#[link(name = "farm", vers = "2.5", author = "mjh")]
#[link(name = "farm", vers = "2.5", author = "mjh")];
mod cow;
mod chicken;
mod horse;
@ -90,7 +90,7 @@ local name `myfarm`.
Our example crate declared this set of `link` attributes:
#[link(name = "farm", vers = "2.5", author = "mjh")]
#[link(name = "farm", vers = "2.5", author = "mjh")];
The version does not match the one provided in the `use` directive, so
unless the compiler can find another crate with the right version
@ -102,14 +102,13 @@ Now for something that you can actually compile yourself. We have
these two files:
// mylib.rs
#[link(name = "mylib", vers = "1.0")];
fn world() -> str { "world" }
// main.rs
use mylib;
fn main() { log_err "hello " + mylib::world(); }
FIXME the compiler currently complains about missing link metas when you compile this
Now compile and run like this (adjust to your platform if necessary):
> rustc --lib mylib.rs

View File

@ -9,3 +9,4 @@ generic
mod
ffi
task
test

View File

@ -10,8 +10,8 @@ Rust program files are, by convention, given the extension `.rs`. Say
we have a file `hello.rs` containing this program:
use std;
fn main() {
std::io::println("hello world!");
fn main(args: [str]) {
std::io::println("hello world from " + args[0] + "!");
}
If the Rust compiler was installed successfully, running `rustc
@ -30,7 +30,16 @@ into an error.
## Anatomy of a Rust program
FIXME say something about libs, main, modules, use
In its simplest form, a Rust program is simply a `.rs` file with some
types and functions defined in it. If it has a `main` function, it can
be compiled to an executable. Rust does not allow code that's not a
declaration to appear at the top level of the file—all statements must
live inside a function.
Rust programs can also be compiled as libraries, and included in other
programs. The `use std` directive that appears at the top of a lot of
examples imports the standard library. This is described in more
detail [later on](mod.html).
## Editing Rust code

View File

@ -280,4 +280,28 @@ exists, convert the result of the expression to the given type.
## Attributes
FIXME Briefly introduce attributes
Every definition can be annotated with attributes. Attributes are meta
information that can serve a variety of purposes. One of those is
conditional compilation:
#[cfg(target_os = "win32")]
fn register_win_service() { /* ... */ }
This will cause the function to vanish without a trace during
compilation on a non-Windows platform. Attributes always look like
`#[attr]`, where `attr` can be simply a name (as in `#[test]`, which
is used by the [built-in test framework](test.html)), a name followed
by `=` and then a literal (as in `#[license = "BSD"]`, which is a
valid way to annotate a Rust program as being released under a
BSD-style license), or a name followed by a comma-separated list of
nested attributes, as in the `cfg` example above.
An attribute without a semicolon following it applies to the
definition that follows it. When terminated with a semicolon, it
applies to the current context. The above example could also be
written like this:
fn register_win_service() {
#[cfg(target_os = "win32")];
/* ... */
}

3
doc/tutorial/test.md Normal file
View File

@ -0,0 +1,3 @@
# Testing
FIXME to be written