Skip to content

Do not use "nil" to refer to () #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2020
Merged
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
4 changes: 2 additions & 2 deletions src/custom_types/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Person<'a> {
}

// A unit struct
struct Nil;
struct Unit;

// A tuple struct
struct Pair(i32, f32);
Expand Down Expand Up @@ -70,7 +70,7 @@ fn main() {
};

// Instantiate a unit struct
let _nil = Nil;
let _unit = Unit;

// Instantiate a tuple struct
let pair = Pair(1, 0.1);
Expand Down
24 changes: 12 additions & 12 deletions src/trait/clone.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Clone

When dealing with resources, the default behavior is to transfer them during
assignments or function calls. However, sometimes we need to make a
assignments or function calls. However, sometimes we need to make a
copy of the resource as well.

The [`Clone`][clone] trait helps us do exactly this. Most commonly, we can
The [`Clone`][clone] trait helps us do exactly this. Most commonly, we can
use the `.clone()` method defined by the `Clone` trait.

```rust,editable
// A unit struct without resources
#[derive(Debug, Clone, Copy)]
struct Nil;
struct Unit;

// A tuple struct with resources that implements the `Clone` trait
#[derive(Clone, Debug)]
struct Pair(Box<i32>, Box<i32>);

fn main() {
// Instantiate `Nil`
let nil = Nil;
// Copy `Nil`, there are no resources to move
let copied_nil = nil;
// Instantiate `Unit`
let unit = Unit;
// Copy `Unit`, there are no resources to move
let copied_unit = unit;

// Both `Nil`s can be used independently
println!("original: {:?}", nil);
println!("copy: {:?}", copied_nil);
// Both `Unit`s can be used independently
println!("original: {:?}", unit);
println!("copy: {:?}", copied_unit);

// Instantiate `Pair`
let pair = Pair(Box::new(1), Box::new(2));
Expand All @@ -37,7 +37,7 @@ fn main() {
// Error! `pair` has lost its resources
//println!("original: {:?}", pair);
// TODO ^ Try uncommenting this line

// Clone `moved_pair` into `cloned_pair` (resources are included)
let cloned_pair = moved_pair.clone();
// Drop the original pair using std::mem::drop
Expand All @@ -52,4 +52,4 @@ fn main() {
}
```

[clone]: https://doc.rust-lang.org/std/clone/trait.Clone.html
[clone]: https://doc.rust-lang.org/std/clone/trait.Clone.html