Two examples of simple paths consisting of only identifier components:
-~~~~
+~~~~{.xfail-test}
x;
x::y::z;
~~~~
Two examples of paths with type arguments:
-~~~~
+~~~~{.xfail-test}
type t = map::hashtbl<int,str>; // Type arguments used in a type expression
let x = id::<int>(10); // Type arguments used in a call expression
~~~~
An example of a crate file:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
// Linkage attributes
#[ link(name = "projx"
vers = "2.5",
An example of a module:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
mod math {
type complex = (f64,f64);
fn sin(f64) -> f64 {
Two examples of `use` declarations:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
use pcre (uuid = "54aba0f8-a7b1-4beb-92f1-4cf625264841");
use std; // equivalent to: use std ( name = "std" );
An example of imports:
-~~~~
+~~~~{.xfail-test}
import foo = core::info;
import std::math::sin;
import std::str::{char_at, hash};
An example of an export:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
mod foo {
export primary;
Multiple names may be exported from a single export declaration:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
mod foo {
export primary, secondary;
When exporting the name of an `enum` type `t`, by default, the module also
implicitly exports all of `t`'s constructors. For example:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
mod foo {
export t;
form exports an `enum` type name along with a subset of its
constructors. For example:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
mod foo {
export abstract{};
export slightly_abstract{a, b};
were declared without the `!` annotation, the following code would not
typecheck:
-~~~~
+~~~~{.xfail-test}
fn f(i: int) -> int {
if i == 42 {
ret 42;
A non-boolean function may also be declared with `pure fn`. This allows
predicates to call non-boolean functions as long as they are pure. For example:
-~~~~
+~~~~{.xfail-test}
pure fn pure_length<T>(ls: list<T>) -> uint { /* ... */ }
pure fn nonempty_list<T>(ls: list<T>) -> bool { pure_length(ls) > 0u }
An example of a predicate that uses an unchecked block:
-~~~~
+~~~~{.xfail-test}
fn pure_foldl<T, U: copy>(ls: list<T>, u: U, f: block(&T, &U) -> U) -> U {
alt ls {
nil. { u }
declared, in an angle-bracket-enclosed, comma-separated list following
the function name.
-~~~~
+~~~~{.xfail-test}
fn iter<T>(seq: [T], f: block(T)) {
for elt: T in seq { f(elt); }
}
Crust functions may not be called from Rust code, but their value
may be taken as an unsafe `u8` pointer.
-~~~
+~~~{.xfail-test}
let fptr: *u8 = new_vec;
~~~
An example of an `enum` item and its use:
-~~~~
+~~~~{.xfail-test}
enum animal {
dog;
cat;
An example of a *recursive* `enum` item and its use:
-~~~~
+~~~~{.xfail-test}
enum list<T> {
nil;
cons(T, @list<T>);
_Resources_ are values that have a destructor associated with them. A
_resource item_ is used to declare resource type and constructor.
-~~~~
+~~~~{.xfail-test}
resource file_descriptor(fd: int) {
std::os::libc::close(fd);
}
items](#implementations)_ can be used to provide implementations of
those methods for a specific type.
-~~~~
+~~~~{.xfail-test}
iface shape {
fn draw(surface);
fn bounding_box() -> bounding_box;
These appear after the name, using the same syntax used in [generic
functions](#generic-functions).
-~~~~
+~~~~{.xfail-test}
iface seq<T> {
fn len() -> uint;
fn elt_at(n: uint) -> T;
generic function, the methods of the interface can be called on values
that have the parameter's type. For example:
-~~~~
+~~~~{.xfail-test}
fn draw_twice<T: shape>(surface: surface, sh: T) {
sh.draw(surface);
sh.draw(surface);
implementation of the given interface is in scope) to the interface
type.
-~~~~
+~~~~{.xfail-test}
let myshape: shape = mycircle as shape;
~~~~
An _implementation item_ provides an implementation of an
[interfaces](#interfaces) for a type.
-~~~~
+~~~~{.xfail-test}
type circle = {radius: float, center: point};
impl circle_shape of shape for circle {
implementation targets). In such an implementation, the `of` clause is
not given, and the name is mandatory.
-~~~~
+~~~~{.xfail-test}
impl uint_loops for uint {
fn times(f: block(uint)) {
let i = 0;
are written after the name of the implementation, or if that is not
specified, after the `impl` keyword.
-~~~~
+~~~~{.xfail-test}
impl <T> of seq<T> for [T] {
/* ... */
}
native modules are declared the same as other Rust functions, with the exception
that they may not have a body and are instead terminated by a semi-colon.
-~~~
+~~~{.xfail-test}
native mod c {
fn fopen(filename: *c_char, mod: *c_char) -> *FILE;
}
the standard C "cdecl" ABI. Other ABI's may be specified using the `abi`
attribute as in
-~~~
+~~~{.xfail-test}
// Interface to the Windows API
#[abi = "stdcall"]
native mod kernel32 { }
An example of attributes:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
// A function marked as a unit test
#[test]
fn test_foo() {
~~~~~~~~ {.tuple}
(0f, 4.5f);
-("a", 4u, true)
+("a", 4u, true);
~~~~~~~~
### Record expressions
is bounds-checked at run-time. When the check fails, it will put the
task in a _failing state_.
-~~~~
+~~~~{.xfail-test}
[1, 2, 3, 4][0];
[mutable 'x', 'y'][1] = 'z';
["a", "b"][10]; // fails
An example of an `as` expression:
-~~~~
+~~~~{.xfail-test}
fn avg(v: [float]) -> float {
let sum: float = sum(v);
let sz: float = std::vec::len(v) as float;
An example of three different move expressions:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
x <- a;
x[i] <- b;
x.y <- c;
An example of three different swap expressions:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
x <-> a;
x[i] <-> b[i];
x.y <-> a.b;
expression](#unary-copy-expressions). For example, the following two
expressions have the same effect:
-~~~~
+~~~~{.xfail-test}
x = y
x <- copy y
~~~~
An example of a call expression:
-~~~~
+~~~~{.xfail-test}
let x: int = add(1, 2);
~~~~
An example of a `bind` expression:
-~~~~
+~~~~{.xfail-test}
fn add(x: int, y: int) -> int {
ret x + y;
}
An example of a simple `while` expression:
-~~~~
+~~~~{.xfail-test}
while i < 10 {
print("hello\n");
i = i + 1;
An example of a `do`-`while` expression:
-~~~~
+~~~~{.xfail-test}
do {
print("hello\n");
i = i + 1;
An example a for loop:
-~~~~
+~~~~{.xfail-test}
let v: [foo] = [a, b, c];
for e: foo in v {
An example of an `alt` expression:
-~~~~
+~~~~{.xfail-test}
enum list<X> { nil; cons(X, @list<X>); }
let x: list<int> = cons(10, @cons(11, @nil));
When matching fields of a record, the fields being matched are specified
first, then a placeholder (`_`) represents the remaining fields.
-~~~~
+~~~~{.xfail-test}
fn main() {
let r = {
player: "ralph",
Multiple alternative patterns may be joined with the `|` operator. A
range of values may be specified with `to`. For example:
-~~~~
+~~~~{.xfail-test}
let message = alt x {
0 | 1 { "not many" }
2 to 9 { "a few" }
consist of a bool-typed expression following the `if` keyword. A pattern
guard may refer to the variables bound within the pattern they follow.
-~~~~
+~~~~{.xfail-test}
let message = alt maybe_digit {
some(x) if x < 10 { process_digit(x) }
some(x) { process_other(x) }
An example of a `note` expression:
-~~~~
+~~~~{.xfail-test}
fn read_file_lines(path: str) -> [str] {
note path;
let r: [str];
The following examples all produce the same output, logged at the `error`
logging level:
-~~~~
+~~~~{.xfail-test}
// Full version, logging a value.
log(core::error, "file not found: " + filename);
on subsequent expressions, propagating [conditions](#conditions) forward and
statically comparing implied states and their specifications.
-~~~~~~~~
+~~~~~~~~{.xfail-test}
pure fn even(x: int) -> bool {
ret x & 1 == 0;
}
Example using `check`:
-~~~~
+~~~~{.xfail-test}
check even(x);
print_even(x);
~~~~
Equivalent example using `if check`:
-~~~~
+~~~~{.xfail-test}
if check even(x) {
print_even(x);
} else {
`macro` are pairs (two-element vectors). The pairs consist of an invocation
and the syntax to expand into. An example:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
#macro([#apply[fn, [args, ...]], fn(args, ...)]);
~~~~~~~~
sophisticated example:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
#macro([#zip_literals[[x, ...], [y, ...]), [[x, y], ...]]);
#macro([#unzip_literals[[x, y], ...], [[x, ...], [y, ...]]]);
~~~~~~~~
An example of a tuple type and its use:
-~~~~
+~~~~{.xfail-test}
type pair = (int,str);
let p: pair = (10,"hello");
let (a, b) = p;
vector:
-~~~~
+~~~~{.xfail-test}
let v: mutable [int] = [1, 2, 3];
v += [4, 5, 6];
~~~~
An example of a `fn` type:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
fn add(x: int, y: int) -> int {
ret x + y;
}
be noncopyable, unless one of the special bounds `send` or `copy` is
declared for it. For example, this is not a valid program:
-~~~~
+~~~~{.xfail-test}
fn box<T>(x: T) -> @T { @x }
~~~~
For example, this code:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
s = "hello, world";
print(s);
~~~~~~~~
Whereas this code:
-~~~~~~~~
- print(x() + y());
+~~~~~~~~{.xfail-test}
+print(x() + y());
~~~~~~~~
Consists of 1 statement, 7 expressions and 14 points:
An example of an implicit-dereference operation performed on box values:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
let x: @int = @10;
let y: @int = @12;
assert (x + y == 22);
An example of a `spawn` call:
-~~~~
+~~~~{.xfail-test}
import task::*;
import comm::*;
An example of a send:
-~~~~
+~~~~{.xfail-test}
import comm::*;
let c: chan<str> = ...;
send(c, "hello, world");
An example of a *receive*:
-~~~~~~~~
+~~~~~~~~{.xfail-test}
import comm::*;
let p: port<str> = ...;
let s = recv(p);
$$(Q)rm -f $(1)/test/doc-tutorial/*.rs
$$(Q)$$(EXTRACT_TESTS) $$(S)doc/tutorial.md $(1)/test/doc-tutorial
+doc-ref-extract$(1):
+ @$$(call E, extract: ref tests)
+ $$(Q)rm -f $(1)/test/doc-ref/*.rs
+ $$(Q)$$(EXTRACT_TESTS) $$(S)doc/rust.md $(1)/test/doc-ref
+
endef
$(foreach host,$(CFG_TARGET_TRIPLES), \
check-stage$(1)-T-$(2)-H-$(3)-bench \
check-stage$(1)-T-$(2)-H-$(3)-pretty \
check-stage$(1)-T-$(2)-H-$(3)-rustdoc \
- check-stage$(1)-T-$(2)-H-$(3)-doc-tutorial
+ check-stage$(1)-T-$(2)-H-$(3)-doc-tutorial \
+ check-stage$(1)-T-$(2)-H-$(3)-doc-ref
check-stage$(1)-T-$(2)-H-$(3)-core: \
check-stage$(1)-T-$(2)-H-$(3)-core-dummy
check-stage$(1)-T-$(2)-H-$(3)-doc-tutorial: \
check-stage$(1)-T-$(2)-H-$(3)-doc-tutorial-dummy
+check-stage$(1)-T-$(2)-H-$(3)-doc-ref: \
+ check-stage$(1)-T-$(2)-H-$(3)-doc-ref-dummy
+
# Rules for the core library test runner
$(3)/test/coretest.stage$(1)-$(2)$$(X): \
--build-base $(3)/test/doc-tutorial/ \
--mode run-pass
+DOC_REF_ARGS$(1)-T-$(2)-H-$(3) := \
+ $$(CTEST_COMMON_ARGS$(1)-T-$(2)-H-$(3)) \
+ --src-base $(3)/test/doc-ref/ \
+ --build-base $(3)/test/doc-ref/ \
+ --mode run-pass
+
check-stage$(1)-T-$(2)-H-$(3)-cfail-dummy: \
$$(HBIN$(1)_H_$(3))/compiletest$$(X) \
$$(SREQ$(1)_T_$(2)_H_$(3)) \
$$(Q)$$(call CFG_RUN_CTEST,$(1),$$<,$(3)) \
$$(DOC_TUTORIAL_ARGS$(1)-T-$(2)-H-$(3))
+check-stage$(1)-T-$(2)-H-$(3)-doc-ref-dummy: \
+ $$(HBIN$(1)_H_$(3))/compiletest$$(X) \
+ $$(SREQ$(1)_T_$(2)_H_$(3)) \
+ doc-ref-extract$(3)
+ @$$(call E, run doc-ref: $$<)
+ $$(Q)$$(call CFG_RUN_CTEST,$(1),$$<,$(3)) \
+ $$(DOC_REF_ARGS$(1)-T-$(2)-H-$(3))
+
endef
# Instantiate the template for stage 0, 1, 2, 3
check-stage$(1)-H-$(2)-doc-tutorial: \
$$(foreach target,$$(CFG_TARGET_TRIPLES), \
check-stage$(1)-T-$$(target)-H-$(2)-doc-tutorial)
+check-stage$(1)-H-$(2)-doc-ref: \
+ $$(foreach target,$$(CFG_TARGET_TRIPLES), \
+ check-stage$(1)-T-$$(target)-H-$(2)-doc-ref)
endef
check-stage$(1)-H-all-doc-tutorial: \
$$(foreach target,$$(CFG_TARGET_TRIPLES), \
check-stage$(1)-H-$$(target)-doc-tutorial)
+check-stage$(1)-H-all-doc-ref: \
+ $$(foreach target,$$(CFG_TARGET_TRIPLES), \
+ check-stage$(1)-H-$$(target)-doc-ref)
endef
check-stage$(1)-pretty-pretty: check-stage$(1)-H-$$(CFG_HOST_TRIPLE)-pretty-pretty
check-stage$(1)-rustdoc: check-stage$(1)-H-$$(CFG_HOST_TRIPLE)-rustdoc
check-stage$(1)-doc-tutorial: check-stage$(1)-H-$$(CFG_HOST_TRIPLE)-doc-tutorial
+check-stage$(1)-doc-ref: check-stage$(1)-H-$$(CFG_HOST_TRIPLE)-doc-ref
endef