Skip to content

Commit 0dc48b4

Browse files
committed
Test fixes and rebase conflicts
1 parent 11e265c commit 0dc48b4

File tree

194 files changed

+386
-309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+386
-309
lines changed

src/doc/guide-error-handling.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ for all but the most trivial of situations.
147147
Here's an example of using `Result`:
148148

149149
```rust
150-
#[deriving(Show)]
150+
#[derive(Show)]
151151
enum Version { Version1, Version2 }
152152

153-
#[deriving(Show)]
153+
#[derive(Show)]
154154
enum ParseError { InvalidHeaderLength, InvalidVersion }
155155

156156
fn parse_version(header: &[u8]) -> Result<Version, ParseError> {

src/doc/guide-ffi.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ referenced Rust object.
262262
Rust code:
263263
264264
~~~~no_run
265+
# use std::boxed::Box;
265266
266267
#[repr(C)]
267268
struct RustObject {
@@ -286,7 +287,7 @@ extern {
286287
287288
fn main() {
288289
// Create the object that will be referenced in the callback
289-
let mut rust_object = box RustObject { a: 5 };
290+
let mut rust_object = Box::new(RustObject { a: 5 });
290291
291292
unsafe {
292293
register_callback(&mut *rust_object, callback);

src/doc/guide-ownership.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,29 @@ therefore deallocates the memory for you. Here's the equivalent example in
8181
Rust:
8282

8383
```rust
84+
# use std::boxed::Box;
8485
{
85-
let x = box 5i;
86+
let x = Box::new(5i);
8687
}
8788
```
8889

89-
The `box` keyword creates a `Box<T>` (specifically `Box<int>` in this case) by
90-
allocating a small segment of memory on the heap with enough space to fit an
91-
`int`. But where in the code is the box deallocated? We said before that we
92-
must have a deallocation for each allocation. Rust handles this for you. It
90+
The `Box::new` function creates a `Box<T>` (specifically `Box<int>` in this
91+
case) by allocating a small segment of memory on the heap with enough space to
92+
fit an `int`. But where in the code is the box deallocated? We said before that
93+
we must have a deallocation for each allocation. Rust handles this for you. It
9394
knows that our handle, `x`, is the owning reference to our box. Rust knows that
9495
`x` will go out of scope at the end of the block, and so it inserts a call to
9596
deallocate the memory at the end of the scope. Because the compiler does this
96-
for us, it's impossible to forget. We always have exactly one deallocation paired
97-
with each of our allocations.
97+
for us, it's impossible to forget. We always have exactly one deallocation
98+
paired with each of our allocations.
9899

99100
This is pretty straightforward, but what happens when we want to pass our box
100101
to a function? Let's look at some code:
101102

102103
```rust
104+
# use std::boxed::Box;
103105
fn main() {
104-
let x = box 5i;
106+
let x = Box::new(5i);
105107

106108
add_one(x);
107109
}
@@ -115,8 +117,9 @@ This code works, but it's not ideal. For example, let's add one more line of
115117
code, where we print out the value of `x`:
116118

117119
```{rust,ignore}
120+
# use std::boxed::Box;
118121
fn main() {
119-
let x = box 5i;
122+
let x = Box::new(5i);
120123
121124
add_one(x);
122125
@@ -148,8 +151,9 @@ To fix this, we can have `add_one` give ownership back when it's done with the
148151
box:
149152

150153
```rust
154+
# use std::boxed::Box;
151155
fn main() {
152-
let x = box 5i;
156+
let x = Box::new(5i);
153157

154158
let y = add_one(x);
155159

src/doc/guide-pointers.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,13 @@ fn rc_succ(x: Rc<int>) -> int { *x + 1 }
455455
Note that the caller of your function will have to modify their calls slightly:
456456

457457
```{rust}
458+
# use std::boxed::Box;
458459
use std::rc::Rc;
459460
460461
fn succ(x: &int) -> int { *x + 1 }
461462
462463
let ref_x = &5i;
463-
let box_x = box 5i;
464+
let box_x = Box::new(5i);
464465
let rc_x = Rc::new(5i);
465466
466467
succ(ref_x);
@@ -477,24 +478,17 @@ those contents.
477478
heap allocation in Rust. Creating a box looks like this:
478479

479480
```{rust}
480-
let x = box(std::boxed::HEAP) 5i;
481+
# use std::boxed::Box;
482+
let x = Box::new(5i);
481483
```
482484

483-
`box` is a keyword that does 'placement new,' which we'll talk about in a bit.
484-
`box` will be useful for creating a number of heap-allocated types, but is not
485-
quite finished yet. In the meantime, `box`'s type defaults to
486-
`std::boxed::HEAP`, and so you can leave it off:
487-
488-
```{rust}
489-
let x = box 5i;
490-
```
491-
492-
As you might assume from the `HEAP`, boxes are heap allocated. They are
493-
deallocated automatically by Rust when they go out of scope:
485+
Boxes are heap allocated and they are deallocated automatically by Rust when
486+
they go out of scope:
494487

495488
```{rust}
489+
# use std::boxed::Box;
496490
{
497-
let x = box 5i;
491+
let x = Box::new(5i);
498492
499493
// stuff happens
500494
@@ -513,8 +507,9 @@ You don't need to fully grok the theory of affine types or regions to grok
513507
boxes, though. As a rough approximation, you can treat this Rust code:
514508

515509
```{rust}
510+
# use std::boxed::Box;
516511
{
517-
let x = box 5i;
512+
let x = Box::new(5i);
518513
519514
// stuff happens
520515
}
@@ -553,12 +548,13 @@ for more detail on how lifetimes work.
553548
Using boxes and references together is very common. For example:
554549

555550
```{rust}
551+
# use std::boxed::Box;
556552
fn add_one(x: &int) -> int {
557553
*x + 1
558554
}
559555
560556
fn main() {
561-
let x = box 5i;
557+
let x = Box::new(5i);
562558
563559
println!("{}", add_one(&*x));
564560
}
@@ -570,12 +566,13 @@ function, and since it's only reading the value, allows it.
570566
We can borrow `x` multiple times, as long as it's not simultaneous:
571567

572568
```{rust}
569+
# use std::boxed::Box;
573570
fn add_one(x: &int) -> int {
574571
*x + 1
575572
}
576573
577574
fn main() {
578-
let x = box 5i;
575+
let x = Box::new(5i);
579576
580577
println!("{}", add_one(&*x));
581578
println!("{}", add_one(&*x));
@@ -586,12 +583,13 @@ fn main() {
586583
Or as long as it's not a mutable borrow. This will error:
587584

588585
```{rust,ignore}
586+
# use std::boxed::Box;
589587
fn add_one(x: &mut int) -> int {
590588
*x + 1
591589
}
592590
593591
fn main() {
594-
let x = box 5i;
592+
let x = Box::new(5i);
595593
596594
println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference
597595
// of `&`-pointer as mutable
@@ -612,22 +610,23 @@ Sometimes, you need a recursive data structure. The simplest is known as a
612610

613611

614612
```{rust}
615-
#[deriving(Show)]
613+
# use std::boxed::Box;
614+
#[derive(Show)]
616615
enum List<T> {
617616
Cons(T, Box<List<T>>),
618617
Nil,
619618
}
620619
621620
fn main() {
622-
let list: List<int> = List::Cons(1, box List::Cons(2, box List::Cons(3, box List::Nil)));
621+
let list: List<int> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil))))));
623622
println!("{:?}", list);
624623
}
625624
```
626625

627626
This prints:
628627

629628
```text
630-
Cons(1, box Cons(2, box Cons(3, box Nil)))
629+
Cons(1, Box(Cons(2, Box(Cons(3, Box(Nil))))))
631630
```
632631

633632
The reference to another `List` inside of the `Cons` enum variant must be a box,
@@ -667,6 +666,7 @@ In many languages with pointers, you'd return a pointer from a function
667666
so as to avoid copying a large data structure. For example:
668667

669668
```{rust}
669+
# use std::boxed::Box;
670670
struct BigStruct {
671671
one: int,
672672
two: int,
@@ -675,15 +675,15 @@ struct BigStruct {
675675
}
676676
677677
fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
678-
return box *x;
678+
return Box::new(*x);
679679
}
680680
681681
fn main() {
682-
let x = box BigStruct {
682+
let x = Box::new(BigStruct {
683683
one: 1,
684684
two: 2,
685685
one_hundred: 100,
686-
};
686+
});
687687
688688
let y = foo(x);
689689
}
@@ -695,6 +695,7 @@ than the hundred `int`s that make up the `BigStruct`.
695695
This is an antipattern in Rust. Instead, write this:
696696

697697
```{rust}
698+
# use std::boxed::Box;
698699
struct BigStruct {
699700
one: int,
700701
two: int,
@@ -707,13 +708,13 @@ fn foo(x: Box<BigStruct>) -> BigStruct {
707708
}
708709
709710
fn main() {
710-
let x = box BigStruct {
711+
let x = Box::new(BigStruct {
711712
one: 1,
712713
two: 2,
713714
one_hundred: 100,
714-
};
715+
});
715716
716-
let y = box foo(x);
717+
let y = Box::new(foo(x));
717718
}
718719
```
719720

src/doc/guide-unsafe.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ extern crate libc;
197197
use libc::{c_void, size_t, malloc, free};
198198
use std::mem;
199199
use std::ptr;
200+
# use std::boxed::Box;
200201
201202
// Define a wrapper around the handle returned by the foreign code.
202203
// Unique<T> has the same semantics as Box<T>
@@ -265,7 +266,7 @@ impl<T: Send> Drop for Unique<T> {
265266
// A comparison between the built-in `Box` and this reimplementation
266267
fn main() {
267268
{
268-
let mut x = box 5i;
269+
let mut x = Box::new(5i);
269270
*x = 10;
270271
} // `x` is freed here
271272
@@ -653,7 +654,7 @@ sugar for dynamic allocations via `malloc` and `free`:
653654

654655
```
655656
#![no_std]
656-
#![feature(lang_items)]
657+
#![feature(lang_items, box_syntax)]
657658
658659
extern crate libc;
659660

src/doc/guide.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,18 +3802,19 @@ enum List { // error: illegal recursive enum type
38023802
38033803
But the compiler complains that the type is recursive, that is, it could be
38043804
arbitrarily large. To remedy this, Rust provides a fixed-size container called
3805-
a **box** that can hold any type. You can box up any value with the `box`
3806-
keyword. Our boxed List gets the type `Box<List>` (more on the notation when we
3805+
a **Box** that can hold any type. You can box up any value with the `Box::new`
3806+
function. Our boxed List gets the type `Box<List>` (more on the notation when we
38073807
get to generics):
38083808
38093809
```{rust}
3810+
# use std::boxed::Box;
38103811
enum List {
38113812
Node(u32, Box<List>),
38123813
Nil
38133814
}
38143815
38153816
fn main() {
3816-
let list = List::Node(0, box List::Node(1, box List::Nil));
3817+
let list = List::Node(0, Box::new(List::Node(1, Box::new(List::Nil))));
38173818
}
38183819
```
38193820
@@ -3826,8 +3827,9 @@ just like regular references. This (rather silly) example dynamically allocates
38263827
an integer `5` and makes `x` a pointer to it:
38273828
38283829
```{rust}
3830+
# use std::boxed::Box;
38293831
{
3830-
let x = box 5;
3832+
let x = Box::new(5);
38313833
println!("{}", *x); // Prints 5
38323834
}
38333835
```
@@ -3858,7 +3860,8 @@ Boxes are the sole owner of their contents, so you cannot take a mutable
38583860
reference to them and then use the original box:
38593861
38603862
```{rust,ignore}
3861-
let mut x = box 5;
3863+
# use std::boxed::Box;
3864+
let mut x = Box::new(5);
38623865
let y = &mut x;
38633866
38643867
*x; // you might expect 5, but this is actually an error
@@ -3879,7 +3882,8 @@ As long as `y` is borrowing the contents, we cannot use `x`. After `y` is
38793882
done borrowing the value, we can use it again. This works fine:
38803883
38813884
```{rust}
3882-
let mut x = box 5;
3885+
# use std::boxed::Box;
3886+
let mut x = Box::new(5);
38833887
38843888
{
38853889
let y = &mut x;

0 commit comments

Comments
 (0)