Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ fn main() {
println!("borrow a section of the array as a slice");
analyze_slice(xs.slice(1, 4));

// Out of bound indexing yields a task failure
// Out of bound indexing yields a task panic
println!("{}", xs[5]);
}
4 changes: 2 additions & 2 deletions examples/file/create/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ fn main() {

// Open a file in write-only mode, returns `IoResult<File>`
let mut file = match File::create(&path) {
Err(why) => fail!("couldn't create {}: {}", display, why.desc),
Err(why) => panic!("couldn't create {}: {}", display, why.desc),
Ok(file) => file,
};

// Write the `LOREM_IPSUM` string to `file`, returns `IoResult<()>`
match file.write_str(LOREM_IPSUM) {
Err(why) => {
fail!("couldn't write to {}: {}", display, why.desc)
panic!("couldn't write to {}: {}", display, why.desc)
},
Ok(_) => println!("successfully wrote to {}", display),
}
Expand Down
4 changes: 2 additions & 2 deletions examples/file/open/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ fn main() {
// Open the path in read-only mode, returns `IoResult<File>`
let mut file = match File::open(&path) {
// The `desc` field of `IoError` is a string that describes the error
Err(why) => fail!("couldn't open {}: {}", display, why.desc),
Err(why) => panic!("couldn't open {}: {}", display, why.desc),
Ok(file) => file,
};

// Read the file contents into a string, returns `IoResult<String>`
match file.read_to_string() {
Err(why) => fail!("couldn't read {}: {}", display, why.desc),
Err(why) => panic!("couldn't read {}: {}", display, why.desc),
Ok(string) => print!("{} contains:\n{}", display, string),
}

Expand Down
2 changes: 1 addition & 1 deletion examples/option/input.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sometimes it's desirable to catch the failure of some parts of a program
instead of calling `fail!`, this can be accomplished using the `Option` enum.
instead of calling `panic!`, this can be accomplished using the `Option` enum.

The `Option<T>` enum has two variants:

Expand Down
4 changes: 2 additions & 2 deletions examples/option/option.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// An integer division that doesn't `fail!`
// An integer division that doesn't `panic!`
fn checked_division(dividend: int, divisor: int) -> Option<int> {
if divisor == 0 {
// Failure is represented as the `None` variant
Expand Down Expand Up @@ -31,7 +31,7 @@ fn main() {
let optional_float = Some(0f32);

// The `unwrap` method will extract the value wrapped in a `Some` variant,
// or will `fail!` if called on a `None` variant
// or will `panic!` if called on a `None` variant
println!("{} unwraps to {}", optional_float, optional_float.unwrap());
println!("{} unwraps to {}", none, none.unwrap());
}
14 changes: 7 additions & 7 deletions examples/fail/input.md → examples/panic/input.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
The `fail!` macro can be used to generate a *task* failure and start unwinding
The `panic!` macro can be used to generate a *task* panic and start unwinding
its stack. While unwinding, the runtime will take care of freeing all the
resources *owned* by the task by calling the destructor of all its objects.

Since we are dealing with programs with only one task, `fail!` will cause the
Since we are dealing with programs with only one task, `panic!` will cause the
program to report the failure message and exit.

{fail.play}
{panic.play}

Let's check that `fail!` doesn't leak memory.
Let's check that `panic!` doesn't leak memory.

```
$ rustc fail.rs && valgrind ./fail
$ rustc panic.rs && valgrind ./panic
==2614== Memcheck, a memory error detector
==2614== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2614== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==2614== Command: ./fail
==2614== Command: ./panic
==2614==
task '<main>' failed at 'division by zero', fail.rs:5
task '<main>' panicked at 'division by zero', panic.rs:5
==2614==
==2614== HEAP SUMMARY:
==2614== in use at exit: 0 bytes in 0 blocks
Expand Down
6 changes: 3 additions & 3 deletions examples/fail/fail.rs → examples/panic/panic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Re-implementation of integer division (/)
fn division(dividend: int, divisor: int) -> int {
if divisor == 0 {
// Division by zero triggers a task failure
fail!("division by zero");
// Division by zero triggers a task panic
panic!("division by zero");
} else {
dividend / divisor
}
Expand All @@ -13,7 +13,7 @@ fn main() {
// Heap allocated integer
let _x = box 0i;

// This operation will trigger a task failure
// This operation will trigger a task panic
division(3, 0);

println!("This point won't be reached!");
Expand Down
4 changes: 2 additions & 2 deletions examples/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {

// `stat` returns an IoResult<FileStat> === Result<FileStat, IoError>
let stat = match path.stat() {
Err(why) => fail!("{}", why.desc),
Err(why) => panic!("{}", why.desc),
Ok(stat) => stat,
};

Expand All @@ -36,7 +36,7 @@ fn main() {

// Convert the path into a string slice
match new_path.as_str() {
None => fail!("new path is not a valid UTF-8 sequence"),
None => panic!("new path is not a valid UTF-8 sequence"),
Some(s) => println!("new path is {}", s),
}
}
6 changes: 3 additions & 3 deletions examples/process/pipe/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ static PANGRAM: &'static str =
fn main() {
// Spawn the `wc` command
let mut process = match Command::new("wc").spawn() {
Err(why) => fail!("couldn't spawn wc: {}", why.desc),
Err(why) => panic!("couldn't spawn wc: {}", why.desc),
Ok(process) => process,
};

Expand All @@ -18,7 +18,7 @@ fn main() {

// Write a string to the stdin of `wc`
match stdin.write_str(PANGRAM) {
Err(why) => fail!("couldn't write to wc stdin: {}", why.desc),
Err(why) => panic!("couldn't write to wc stdin: {}", why.desc),
Ok(_) => println!("sent pangram to wc"),
}

Expand All @@ -31,7 +31,7 @@ fn main() {
// the `get_mut_ref` method will return a mutable reference to the value
// wrapped in a `Some` variant
match process.stdout.as_mut().unwrap().read_to_string() {
Err(why) => fail!("couldn't read wc stdout: {}", why.desc),
Err(why) => panic!("couldn't read wc stdout: {}", why.desc),
Ok(string) => print!("wc responded with:\n{}", string),
}
}
2 changes: 1 addition & 1 deletion examples/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
// The `output` method will spawn `rustc --version`, wait until the process
// finishes and return the output of the process
match cmd.output() {
Err(why) => fail!("couldn't spawn rustc: {}", why.desc),
Err(why) => panic!("couldn't spawn rustc: {}", why.desc),
// Destructure `ProcessOutput`
Ok(ProcessOutput { error: err, output: out, status: exit }) => {
// Check if the process succeeded, i.e. the exit code was 0
Expand Down
10 changes: 5 additions & 5 deletions examples/result/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod checked {

pub fn div(x: f64, y: f64) -> MathResult {
if y == 0.0 {
// This operation would `fail`, instead let's return the reason of
// This operation would `panic`, instead let's return the reason of
// the failure wrapped in `Err`
Err(DivisionByZero)
} else {
Expand Down Expand Up @@ -41,18 +41,18 @@ mod checked {
fn op(x: f64, y: f64) -> f64 {
// This is a three level match pyramid!
match checked::div(x, y) {
Err(why) => fail!("{}", why),
Err(why) => panic!("{}", why),
Ok(ratio) => match checked::ln(ratio) {
Err(why) => fail!("{}", why),
Err(why) => panic!("{}", why),
Ok(ln) => match checked::sqrt(ln) {
Err(why) => fail!("{}", why),
Err(why) => panic!("{}", why),
Ok(sqrt) => sqrt,
},
},
}
}

fn main() {
// Will this fail?
// Will this panic?
println!("{}", op(1.0, 10.0));
}
2 changes: 1 addition & 1 deletion examples/result/try/try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod checked {

pub fn op(x: f64, y: f64) {
match op_(x, y) {
Err(why) => fail!(match why {
Err(why) => panic!(match why {
NegativeLogarithm => "logarithm of negative number",
DivisionByZero => "division by zero",
NegativeSquareRoot => "square root of negative number",
Expand Down
6 changes: 3 additions & 3 deletions examples/sockets/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ fn main() {
// First argument is the message to be sent
let message = match args.as_slice() {
[_, ref message] => message.as_slice(),
_ => fail!("wrong number of arguments"),
_ => panic!("wrong number of arguments"),
};

// Connect to socket
let mut stream = match UnixStream::connect(&socket) {
Err(_) => fail!("server is not running"),
Err(_) => panic!("server is not running"),
Ok(stream) => stream,
};

// Send message
match stream.write_str(message) {
Err(_) => fail!("couldn't send message"),
Err(_) => panic!("couldn't send message"),
Ok(_) => {}
}
}
2 changes: 1 addition & 1 deletion examples/sockets/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {

// Bind to socket
let stream = match UnixListener::bind(&socket) {
Err(_) => fail!("failed to bind socket"),
Err(_) => panic!("failed to bind socket"),
Ok(stream) => stream,
};

Expand Down
2 changes: 1 addition & 1 deletion examples/staging/macros/dry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
let mut xs = Vec::from_elem(5, 0f64);
let ys = Vec::from_elem(6, 1f64);

// this operation will fail at runtime
// this operation will panic at runtime
add_assign(&mut xs, &ys);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/staging/rand/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
println!("sample previous slice *with* replacement 10 times");
for _ in range(0u, 10) {
match rng.choose(v.as_slice()) {
None => fail!("slice was empty"),
None => panic!("slice was empty"),
Some(x) => println!("{}", x),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/staging/test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ no parameters and return nothing.

{unit-test.rs}

If you want the test to fail, just put `#[should_fail]` under `#[test]`.
If you want the test to panic, just put `#[should_fail]` under `#[test]`.

{fail.rs}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{ "id": "enum", "title": "Enums", "children": [
{ "id": "c-like", "title": "C-like", "children": null }
] },
{ "id": "fail", "title": "`fail!`", "children": null },
{ "id": "panic", "title": "`panic!`", "children": null },
{ "id": "option", "title": "`Option`", "children": null },
{ "id": "array", "title": "Arrays and Slices", "children": null },
{ "id": "trait", "title": "Traits", "children": [
Expand Down
6 changes: 3 additions & 3 deletions src/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub struct Example {
impl Example {
pub fn get_list() -> Vec<Example> {
match file::read(&Path::new("examples/structure.json")) {
Err(why) => fail!("{}", why),
Err(why) => panic!("{}", why),
Ok(string) => match json::from_str(string.as_slice()) {
Err(_) => fail!("structure.json is not valid json"),
Err(_) => panic!("structure.json is not valid json"),
Ok(json) => {
match Decodable::decode(&mut json::Decoder::new(json)) {
Err(_) => fail!("error decoding structure.json"),
Err(_) => panic!("error decoding structure.json"),
Ok(examples) => examples,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
.connect("\n");

match file::write(&Path::new("stage/SUMMARY.md"), summary.as_slice()) {
Err(why) => fail!("{}", why),
Err(why) => panic!("{}", why),
Ok(_) => {},
}
}