{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/book/pulls/comments/111410235","pull_request_review_id":32654633,"id":111410235,"diff_hunk":"@@ -0,0 +1,340 @@\n+## Advanced Types\n+\n+The Rust type system has some features that we've mentioned or used without\n+discussing. We started talking about the newtype pattern in regards to traits;\n+we'll start with a more general discussion about why newtypes are useful as\n+types. We'll then move to type aliases, a feature that is similar to newtypes\n+but has slightly different semantics. We'll also discuss the `!` type and\n+dynamically sized types.\n+\n+### Using the Newtype Pattern for Type Safety and Abstraction\n+\n+The newtype pattern that we started discussing at the end of the \"Advanced\n+Traits\" section, where we create a new type as a tuple struct with one field\n+that wraps a type can also be useful for statically enforcing that values are\n+never confused, and is often used to indicate the units of a value. We actually\n+had an example of this in Listing 19-26: the `Millimeters` and `Meters` structs\n+both wrap `u32` values in a new type. If we write a function with a parameter\n+of type `Millimeters`, we won't be able to compile a program that accidentally\n+tries to call that function with a value of type `Meters` or a plain `u32`.\n+\n+Another reason to use the newtype pattern is to abstract away some\n+implementation details of a type: the wrapper type can expose a different\n+public API than the private inner type would if we used it directly in order to\n+restrict the functionality that is available, for example. New types can also\n+hide internal generic types. For example, we could provide a `People` type that\n+wraps a `HashMap<i32, String>` that stores a person's ID associated with their\n+name. Code using `People` would only interact with the public API we provide,\n+such as a method to add a name string to the `People` collection, and that code\n+wouldn't need to know that we assign an `i32` ID to names internally. The\n+newtype pattern is a lightweight way to achieve encapsulation to hide\n+implementation details that we discussed in Chapter 17.\n+\n+### Type Aliases Create Type Synonyms\n+\n+The newtype pattern involves creating a new struct to be a new, separate type.\n+Rust also provides the ability to declare a *type alias* with the `type`\n+keyword to give an existing type another name. For example, we can create the\n+alias `Kilometers` to `i32` like so:\n+\n+```rust\n+type Kilometers = i32;\n+```\n+\n+This means `Kilometers` is a *synonym* for `i32`; unlike the `Millimeters` and\n+`Meters` types we created in Listing 19-26, `Kilometers` is not a separate, new\n+type. Values that have the type `Kilometers` will be treated exactly the same\n+as values of type `i32`:\n+\n+```rust\n+type Kilometers = i32;\n+\n+let x: i32 = 5;\n+let y: Kilometers = 5;\n+\n+println!(\"x + y = {}\", x + y);\n+```\n+\n+Since `Kilometers` is an alias for `i32`, they're the same type. We can add\n+values of type `i32` and `Kilometers` together, and we can pass `Kilometers`\n+values to functions that take `i32` parameters. We don't get the type checking\n+benefits that we get from the newtype pattern that we discussed in the previous\n+section.\n+\n+The main use case for type synonyms is to reduce repetition. For example, we\n+may have a lengthy type like this:\n+\n+```rust,ignore\n+Box<FnOnce() + Send + 'static>","path":"second-edition/src/ch19-04-advanced-types.md","position":68,"original_position":68,"commit_id":"90bc1e90ba1edd3656ae358980f9ac8f8ce7db0f","original_commit_id":"acdb8ec32681c6e952129dad43bc8d8663930c2d","user":{"login":"carols10cents","id":193874,"avatar_url":"https://avatars1.githubusercontent.com/u/193874?v=3","gravatar_id":"","url":"https://api.github.com/users/carols10cents","html_url":"https://github.com/carols10cents","followers_url":"https://api.github.com/users/carols10cents/followers","following_url":"https://api.github.com/users/carols10cents/following{/other_user}","gists_url":"https://api.github.com/users/carols10cents/gists{/gist_id}","starred_url":"https://api.github.com/users/carols10cents/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carols10cents/subscriptions","organizations_url":"https://api.github.com/users/carols10cents/orgs","repos_url":"https://api.github.com/users/carols10cents/repos","events_url":"https://api.github.com/users/carols10cents/events{/privacy}","received_events_url":"https://api.github.com/users/carols10cents/received_events","type":"User","site_admin":false},"body":"I'm going to table this for now and file an issue to think about changing this next edit round :)","created_at":"2017-04-13T14:58:47Z","updated_at":"2017-04-13T14:58:47Z","html_url":"https://github.com/rust-lang/book/pull/480#discussion_r111410235","pull_request_url":"https://api.github.com/repos/rust-lang/book/pulls/480","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/book/pulls/comments/111410235"},"html":{"href":"https://github.com/rust-lang/book/pull/480#discussion_r111410235"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/book/pulls/480"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/book/pulls/480","id":108185854,"html_url":"https://github.com/rust-lang/book/pull/480","diff_url":"https://github.com/rust-lang/book/pull/480.diff","patch_url":"https://github.com/rust-lang/book/pull/480.patch","issue_url":"https://api.github.com/repos/rust-lang/book/issues/480","number":480,"state":"open","locked":false,"title":"Chapter 19: advanced features","user":{"login":"steveklabnik","id":27786,"avatar_url":"https://avatars1.githubusercontent.com/u/27786?v=3","gravatar_id":"","url":"https://api.github.com/users/steveklabnik","html_url":"https://github.com/steveklabnik","followers_url":"https://api.github.com/users/steveklabnik/followers","following_url":"https://api.github.com/users/steveklabnik/following{/other_user}","gists_url":"https://api.github.com/users/steveklabnik/gists{/gist_id}","starred_url":"https://api.github.com/users/steveklabnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/steveklabnik/subscriptions","organizations_url":"https://api.github.com/users/steveklabnik/orgs","repos_url":"https://api.github.com/users/steveklabnik/repos","events_url":"https://api.github.com/users/steveklabnik/events{/privacy}","received_events_url":"https://api.github.com/users/steveklabnik/received_events","type":"User","site_admin":false},"body":"I feel like in the sprint to the end of the book, this is a bit light. \r\n\r\n@aturon an extra review by you here would be nice too ❤️ ","created_at":"2017-02-27T19:39:37Z","updated_at":"2017-04-13T14:58:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"90cd0502cdfe3352f024ed7db406b14857004b1a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/book/pulls/480/commits","review_comments_url":"https://api.github.com/repos/rust-lang/book/pulls/480/comments","review_comment_url":"https://api.github.com/repos/rust-lang/book/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/book/issues/480/comments","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/90bc1e90ba1edd3656ae358980f9ac8f8ce7db0f","head":{"label":"rust-lang:ch19-advanced-features.md","ref":"ch19-advanced-features.md","sha":"90bc1e90ba1edd3656ae358980f9ac8f8ce7db0f","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":47854924,"name":"book","full_name":"rust-lang/book","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/book","description":"The Rust Programming Language","fork":false,"url":"https://api.github.com/repos/rust-lang/book","forks_url":"https://api.github.com/repos/rust-lang/book/forks","keys_url":"https://api.github.com/repos/rust-lang/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/book/teams","hooks_url":"https://api.github.com/repos/rust-lang/book/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/book/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/book/events","assignees_url":"https://api.github.com/repos/rust-lang/book/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/book/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/book/tags","blobs_url":"https://api.github.com/repos/rust-lang/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/book/languages","stargazers_url":"https://api.github.com/repos/rust-lang/book/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/book/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/book/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/book/subscription","commits_url":"https://api.github.com/repos/rust-lang/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/book/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/book/merges","archive_url":"https://api.github.com/repos/rust-lang/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/book/downloads","issues_url":"https://api.github.com/repos/rust-lang/book/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/book/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/book/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/book/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/book/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/book/deployments","created_at":"2015-12-11T22:49:49Z","updated_at":"2017-04-13T05:43:06Z","pushed_at":"2017-04-13T04:41:38Z","git_url":"git://github.com/rust-lang/book.git","ssh_url":"git@github.com:rust-lang/book.git","clone_url":"https://github.com/rust-lang/book.git","svn_url":"https://github.com/rust-lang/book","homepage":"http://rust-lang.github.io/book/","size":15145,"stargazers_count":748,"watchers_count":748,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":194,"mirror_url":null,"open_issues_count":66,"forks":194,"open_issues":66,"watchers":748,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"2a9b2a1b019ad6d4832ff3e56fbcba5be68b250e","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":47854924,"name":"book","full_name":"rust-lang/book","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars2.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/book","description":"The Rust Programming Language","fork":false,"url":"https://api.github.com/repos/rust-lang/book","forks_url":"https://api.github.com/repos/rust-lang/book/forks","keys_url":"https://api.github.com/repos/rust-lang/book/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/book/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/book/teams","hooks_url":"https://api.github.com/repos/rust-lang/book/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/book/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/book/events","assignees_url":"https://api.github.com/repos/rust-lang/book/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/book/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/book/tags","blobs_url":"https://api.github.com/repos/rust-lang/book/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/book/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/book/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/book/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/book/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/book/languages","stargazers_url":"https://api.github.com/repos/rust-lang/book/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/book/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/book/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/book/subscription","commits_url":"https://api.github.com/repos/rust-lang/book/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/book/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/book/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/book/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/book/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/book/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/book/merges","archive_url":"https://api.github.com/repos/rust-lang/book/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/book/downloads","issues_url":"https://api.github.com/repos/rust-lang/book/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/book/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/book/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/book/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/book/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/book/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/book/deployments","created_at":"2015-12-11T22:49:49Z","updated_at":"2017-04-13T05:43:06Z","pushed_at":"2017-04-13T04:41:38Z","git_url":"git://github.com/rust-lang/book.git","ssh_url":"git@github.com:rust-lang/book.git","clone_url":"https://github.com/rust-lang/book.git","svn_url":"https://github.com/rust-lang/book","homepage":"http://rust-lang.github.io/book/","size":15145,"stargazers_count":748,"watchers_count":748,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":194,"mirror_url":null,"open_issues_count":66,"forks":194,"open_issues":66,"watchers":748,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/book/pulls/480"},"html":{"href":"https://github.com/rust-lang/book/pull/480"},"issue":{"href":"https://api.github.com/repos/rust-lang/book/issues/480"},"comments":{"href":"https://api.github.com/repos/rust-lang/book/issues/480/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/book/pulls/480/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/book/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/book/pulls/480/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/book/statuses/90bc1e90ba1edd3656ae358980f9ac8f8ce7db0f"}}}}
{"actor":{"display_login":"carols10cents"}}