├── .all-contributorsrc
├── .github
└── workflows
│ └── deploy.yml
├── .gitignore
├── LICENSE
├── README.md
├── Translators.md
├── book.toml
├── depoly.sh
├── package.json
├── scripts
├── fix-name.js
├── fix-reference.js
└── fix-summary.js
├── src
├── MOVE语言术语中英文对照表.md
├── SUMMARY.md
├── abilities.md
├── abort-and-assert.md
├── address.md
├── bool.md
├── coding-conventions.md
├── conditionals.md
├── constants.md
├── diagrams
│ ├── move_state.png
│ └── solidity_state.png
├── equality.md
├── friends.md
├── functions.md
├── generics.md
├── global-storage-operators.md
├── global-storage-structure.md
├── integers.md
├── introduction.md
├── loops.md
├── modules-and-scripts.md
├── move-tutorial.md
├── packages.md
├── references.md
├── signer.md
├── standard-library.md
├── step_1
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── FirstModule.move
├── step_2
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── FirstModule.move
├── step_2_sol
│ ├── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ │ └── FirstModule.move
│ └── solution_commands
├── step_3
│ └── BasicCoin.move
├── step_4
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_4_sol
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_5
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_5_sol
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_6
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ ├── BasicCoin.move
│ │ └── MyOddCoin.move
├── step_7
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_8
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── step_8_sol
│ └── BasicCoin
│ │ ├── Move.toml
│ │ └── sources
│ │ └── BasicCoin.move
├── structs-and-resources.md
├── tuples.md
├── unit-testing.md
├── uses.md
├── variables.md
└── vector.md
└── yarn.lock
/.all-contributorsrc:
--------------------------------------------------------------------------------
1 | {
2 | "projectName": "move-book-zh",
3 | "projectOwner": "move-dao",
4 | "repoType": "github",
5 | "repoHost": "https://github.com",
6 | "files": [
7 | "README.md"
8 | ],
9 | "imageSize": 100,
10 | "commit": true,
11 | "commitConvention": "none",
12 | "skipCi": "true",
13 | "contributors": [
14 | {
15 | "login": "Kusou1",
16 | "name": "zhang",
17 | "avatar_url": "https://avatars.githubusercontent.com/u/57334674?v=4",
18 | "profile": "https://github.com/Kusou1",
19 | "contributions": [
20 | "code",
21 | "doc",
22 | "translation",
23 | "infra"
24 | ]
25 | },
26 | {
27 | "login": "ruy1su",
28 | "name": "ruyisu",
29 | "avatar_url": "https://avatars.githubusercontent.com/u/9391802?v=4",
30 | "profile": "https://github.com/ruy1su",
31 | "contributions": [
32 | "code",
33 | "translation",
34 | "doc",
35 | "review"
36 | ]
37 | },
38 | {
39 | "login": "lshoo",
40 | "name": "lshoo",
41 | "avatar_url": "https://avatars.githubusercontent.com/u/670440?v=4",
42 | "profile": "https://github.com/lshoo",
43 | "contributions": [
44 | "code",
45 | "translation",
46 | "doc",
47 | "ideas",
48 | "review"
49 | ]
50 | },
51 | {
52 | "login": "Container-00",
53 | "name": "Container",
54 | "avatar_url": "https://avatars.githubusercontent.com/u/61052480?v=4",
55 | "profile": "https://github.com/Container-00",
56 | "contributions": [
57 | "code",
58 | "translation",
59 | "doc",
60 | "review"
61 | ]
62 | },
63 | {
64 | "login": "nosalt99",
65 | "name": "nosalt",
66 | "avatar_url": "https://avatars.githubusercontent.com/u/22558493?v=4",
67 | "profile": "https://github.com/nosalt99",
68 | "contributions": [
69 | "code",
70 | "translation",
71 | "doc",
72 | "infra"
73 | ]
74 | },
75 | {
76 | "login": "stephenreborn",
77 | "name": "stephenreborn",
78 | "avatar_url": "https://avatars.githubusercontent.com/u/6388610?v=4",
79 | "profile": "https://github.com/stephenreborn",
80 | "contributions": [
81 | "code",
82 | "translation",
83 | "talk"
84 | ]
85 | },
86 | {
87 | "login": "666thi",
88 | "name": "666thi",
89 | "avatar_url": "https://avatars.githubusercontent.com/u/109965699?v=4",
90 | "profile": "https://github.com/666thi",
91 | "contributions": [
92 | "code",
93 | "translation",
94 | "talk"
95 | ]
96 | },
97 | {
98 | "login": "MagicGordon",
99 | "name": "MagicGordon",
100 | "avatar_url": "https://avatars.githubusercontent.com/u/19465870?v=4",
101 | "profile": "https://github.com/MagicGordon",
102 | "contributions": [
103 | "code",
104 | "translation",
105 | "talk"
106 | ]
107 | },
108 | {
109 | "login": "xixifusi1984",
110 | "name": "xixifusi1984",
111 | "avatar_url": "https://avatars.githubusercontent.com/u/39210551?v=4",
112 | "profile": "https://github.com/xixifusi1984",
113 | "contributions": [
114 | "code",
115 | "translation",
116 | "talk"
117 | ]
118 | },
119 | {
120 | "login": "yvvw",
121 | "name": "yvvw",
122 | "avatar_url": "https://avatars.githubusercontent.com/u/15168529?v=4",
123 | "profile": "https://github.com/yvvw",
124 | "contributions": [
125 | "code",
126 | "translation",
127 | "talk"
128 | ]
129 | },
130 | {
131 | "login": "xiaochuan891102",
132 | "name": "xiaochuan891102",
133 | "avatar_url": "https://avatars.githubusercontent.com/u/109952533?v=4",
134 | "profile": "https://github.com/xiaochuan891102",
135 | "contributions": [
136 | "code",
137 | "translation",
138 | "talk"
139 | ]
140 | },
141 | {
142 | "login": "stephenLee",
143 | "name": "stephenLee",
144 | "avatar_url": "https://avatars.githubusercontent.com/u/1144508?v=4",
145 | "profile": "https://github.com/stephenLee",
146 | "contributions": [
147 | "code",
148 | "translation",
149 | "talk"
150 | ]
151 | }
152 | ],
153 | "contributorsPerLine": 6
154 | }
155 |
--------------------------------------------------------------------------------
/.github/workflows/deploy.yml:
--------------------------------------------------------------------------------
1 | name: Deploy
2 | on:
3 | push:
4 | branches:
5 | - main
6 |
7 | jobs:
8 | deploy:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 | with:
13 | fetch-depth: 0
14 | - name: Install mdbook
15 | run: |
16 | mkdir mdbook
17 | curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.14/mdbook-v0.4.14-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
18 | echo `pwd`/mdbook >> $GITHUB_PATH
19 | - name: Deploy GitHub Pages
20 | run: |
21 | # This assumes your book is in the root of your repository.
22 | # Just add a `cd` here if you need to change to another directory.
23 | mdbook build
24 | git worktree add gh-pages
25 | git config user.name "Deploy from CI"
26 | git config user.email ""
27 | cd gh-pages
28 | # Delete the ref to avoid keeping history.
29 | git update-ref -d refs/heads/gh-pages
30 | rm -rf *
31 | mv ../book/* .
32 | git add .
33 | git commit -m "Deploy $GITHUB_SHA to gh-pages"
34 | git push --force --set-upstream origin gh-pages
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | book
2 | node_modules
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Move book 中文版](https://move-dao.github.io/move-book-zh/)
2 |
3 | ## [Move book 目录][moveBookDir]
4 |
5 | [各章节译者](Translators.md)
6 |
7 | [moveBookDir]: https://github.com/move-dao/move-book-zh/blob/main/src/SUMMARY.md
8 |
9 |
10 | [](#contributors-)
11 |
12 | 
13 |
14 |
15 | > Chinese translation of [The Move Language][github-en]
16 |
17 | [github-en]: https://github.com/move-language/move
18 |
19 | ## 依赖
20 |
21 | 构建本书需要 [mdBook],执行以下命令安装:
22 |
23 | [mdBook]: https://github.com/rust-lang-nursery/mdBook
24 |
25 | ```bash
26 | $ cargo install --git https://github.com/rust-lang/mdBook.git mdbook
27 | ```
28 |
29 | ## 构建
30 |
31 | 构建此书,请输入:
32 |
33 | ```
34 | $ mdbook build
35 | ```
36 |
37 | 输出内容存放在 `book` 子目录中。可使用浏览器打开来查看内容。
38 |
39 | ## 预览
40 |
41 | 构建后,输入以下命令即可在本地预览此书,默认情况下在`localhost:3000`:
42 |
43 | ```
44 | $ mdbook serve
45 | ```
46 |
47 | `serve` command watches the book's `src` directory for changes, rebuilding the book and refreshing clients for each change; this includes re-creating deleted files still mentioned in `SUMMARY.md`! A websocket connection is used to trigger the client-side refresh.
48 |
49 |
50 |
51 | ## Contributors ✨
52 |
53 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
54 |
55 |
56 |
57 |
58 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/Translators.md:
--------------------------------------------------------------------------------
1 | | | 章节 | 译者 | 校对 |
2 | | -- | -------------------------- | ---------------------------------------------- | ------------------------------------ |
3 | | 0 | Intoduction | Tom | [@lshoo](https://github.com/lshoo) |
4 | | 1 | Modules and Scripts | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo) |
5 | | 2 | Move Tutorial | | [@lshoo](https://github.com/lshoo) |
6 | | 3 | Integers | Tom | [@lshoo](https://github.com/lshoo) |
7 | | 4 | Bool | Tom | [@lshoo](https://github.com/lshoo) |
8 | | 5 | Address | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo) |
9 | | 6 | Vector | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo) |
10 | | 7 | Signer | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo) |
11 | | 8 | References | container | [@lshoo](https://github.com/lshoo) |
12 | | 9 | Tuples and Unit | container | [@lshoo](https://github.com/lshoo) |
13 | | 10 | Local Variables and Scopes | @ruyisu | [@lshoo](https://github.com/lshoo) |
14 | | 11 | Equality | @ruyisu | [@lshoo](https://github.com/lshoo) |
15 | | 12 | Abort and Assert | @ruyisu | [@lshoo](https://github.com/lshoo) |
16 | | 13 | Conditionals | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo) |
17 | | 14 | While and Loop | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo) |
18 | | 15 | Functions | @nosalt99 | [@lshoo](https://github.com/lshoo) |
19 | | 16 | Structs and Resource | @nosalt99 | [@lshoo](https://github.com/lshoo) |
20 | | 17 | Constants | @nosalt99 | [@lshoo](https://github.com/lshoo) |
21 | | 18 | Generics | 小川 | [@lshoo](https://github.com/lshoo) |
22 | | 19 | Type Abilities | 小川 | [@lshoo](https://github.com/lshoo) |
23 | | 20 | Uses and Aliases | 小川 | [@ruyisu](https://github.com/ruy1su) |
24 | | 21 | Friends | @xiaochuan891102 | [@ruyisu](https://github.com/ruy1su) |
25 | | 22 | Packages | @xiaochuan891102 | [@ruyisu](https://github.com/ruy1su) |
26 | | 23 | Unit Test | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su) |
27 | | 24 | Global Storage Structure | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su) |
28 | | 25 | Global Storage Operators | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su) |
29 | | 26 | Standard Library | @MagicGordon | [@ruyisu](https://github.com/ruy1su) |
30 | | 27 | Coding Conventions | @MagicGordon | [@ruyisu](https://github.com/ruy1su) |
31 |
--------------------------------------------------------------------------------
/book.toml:
--------------------------------------------------------------------------------
1 | [book]
2 | title = "Move Book 中文版"
3 | authors = ["The Move China Contributors"]
4 | language = "zh"
5 | multilingual = false
6 | src = "src"
7 |
8 | [output.html]
9 | git-repository-url = "https://github.com/move-dao/move-book-zh"
10 | git-repository-icon = "fa-github"
--------------------------------------------------------------------------------
/depoly.sh:
--------------------------------------------------------------------------------
1 | ## this script deploys the static website of move-book-cn to github pages
2 |
3 | ## build static website for book
4 | mdbook build
5 |
6 | ## init git repo
7 | cd book
8 | git init
9 | git config user.name "move-dao"
10 | git config user.email "move-dao@gmail.com"
11 | git add .
12 | git commit -m 'deploy'
13 | git branch -M gh-pages
14 | git remote add origin https://github.com/move-dao/move-book-zh.git
15 |
16 | ## push to github pages
17 | git push -u -f origin gh-pages
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "devDependencies": {
3 | "all-contributors-cli": "^6.20.0"
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/scripts/fix-name.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | const fs = require('fs')
4 |
5 | async function main() {
6 | const basePath = 'src'
7 | const dirs = fs.readdirSync(`${basePath}`)
8 | const refs = dirs.reduce((acc, dir) => {
9 | const res = dir.match(/^chapter_\d+_(.*)$/)
10 | if (res === null) return acc
11 | acc.push(res)
12 | return acc
13 | }, [])
14 | for (const dir of dirs) {
15 | const filePath = `${basePath}/${dir}`
16 | if (fs.lstatSync(filePath).isDirectory()) continue
17 |
18 | for (const ref of refs) {
19 | fs.rename(`${basePath}/${ref[0]}`, `${basePath}/${ref[1]}`, err => {
20 | throw err
21 | })
22 | }
23 | }
24 | console.log('fix name done.')
25 | }
26 |
27 | main().catch(err => {
28 | console.error(err)
29 | process.exit(1)
30 | })
--------------------------------------------------------------------------------
/scripts/fix-reference.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | const fs = require('fs')
4 |
5 | async function main() {
6 | const basePath = 'src'
7 | const dirs = fs.readdirSync(`${basePath}`)
8 | const refs = dirs.reduce((acc, dir) => {
9 | const res = dir.match(/^chapter_\d+_(.*)$/)
10 | if (res === null) return acc
11 | acc.push(res)
12 | return acc
13 | }, [])
14 | for (const dir of dirs) {
15 | const filePath = `${basePath}/${dir}`
16 | if (fs.lstatSync(filePath).isDirectory()) continue
17 | const fileOld = fs.readFileSync(filePath).toString()
18 | let fileNew = fileOld
19 | for (const ref of refs) {
20 | fileNew = fileNew.replaceAll(`./${ref[0]}`, `./${ref[1]}`)
21 | }
22 | if (fileNew !== fileOld) {
23 | fs.writeFileSync(filePath, fileNew)
24 | }
25 | }
26 | console.log('fix reference done.')
27 | }
28 |
29 | main().catch(err => {
30 | console.error(err)
31 | process.exit(1)
32 | })
33 |
--------------------------------------------------------------------------------
/scripts/fix-summary.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | const fs = require('fs')
4 |
5 | async function main() {
6 | const basePath = 'src'
7 | let newSummary = fs.readFileSync(`./${basePath}/SUMMARY.md`).toString().replace(/chapter_\d+_/g,"")
8 | fs.writeFileSync(`${basePath}/SUMMARY.md`, newSummary)
9 | console.log('fix summary done.')
10 | }
11 |
12 | main().catch(err => {
13 | console.error(err)
14 | process.exit(1)
15 | })
--------------------------------------------------------------------------------
/src/MOVE语言术语中英文对照表.md:
--------------------------------------------------------------------------------
1 | Move 语言术语中英文对照表由 Move-Dao 中文翻译项目组提供,致力于实现 Move 的文档和书籍中文的术语都保持一致性,本表内容将持续更新维护。所有的对 Move 翻译的学生、开发者或编者都应该参照本表的有关术语。
2 |
3 | | English 英文 | Chinese 中文 | Note 备注 |
4 | | ------ | --------- | --------------------------------------------------------------------------- |
5 | | A | | |
6 | | argument | 参数,实参,实际参数 | 不严格区分的话, argument(参数)和 parameter(参量)可以互换地使用 |
7 | | ability | 能力 | |
8 | | annotate | 标注,注解 | |
9 | | B | |
10 | | backing out | 撤销,回滚 | |
11 | | behavior | 特性,行为 | |
12 | | C | |
13 | | copy | 副本 | |
14 | | D | |
15 | | declare, declaration | 声明 | |
16 | | destroy | 销毁 | explicitly destroyed 我翻译为显式销毁 |
17 | | drop | 掉落 | |
18 | | F | |
19 | | field | 字段 | |
20 | | M | |
21 | | mutate | 变更 | |
22 | | mutation | 变更 | |
23 | | S | |
24 | | scope | 作用域 | |
25 | | statement | 语句 | |
26 | | T | |
27 | | type | 类型 | |
28 | | V | | |
29 | | variable | 变量 | |
30 | | variable binding | 变量绑定 | |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # The Move Programming Language
2 |
3 | [引言 (Introduction)](introduction.md)
4 |
5 | ## Getting Started
6 |
7 | - [模块和脚本 (Modules and Scripts)](modules-and-scripts.md)
8 | - [Move 教程 (Move Tutorial)](move-tutorial.md)
9 |
10 | ## Primitive Types
11 |
12 | - [整数 (Integers)](integers.md)
13 | - [布尔类型 (Bool) ](bool.md)
14 | - [地址 (Address)](address.md)
15 | - [向量 (Vector)](vector.md)
16 | - [签名 (Signer)](signer.md)
17 | - [引用 (References)](references.md)
18 | - [元组和 Unit (Tuples and Unit)](tuples.md)
19 |
20 | ## Basic Concepts
21 |
22 | - [局部变量和作用域 (Local Variables and Scopes)](variables.md)
23 | - [等式 (Equality)](equality.md)
24 | - [中止和断言 (Abort and Assert)](abort-and-assert.md)
25 | - [条件语句 (Conditionals)](conditionals.md)
26 | - [循环 (While and Loop)](loops.md)
27 | - [函数 (Functions)](functions.md)
28 | - [结构体和资源 (Structs and Resources)](structs-and-resources.md)
29 | - [常量 (Constants)](constants.md)
30 | - [泛型 (Generics)](generics.md)
31 | - [类型能力 (Type Abilities)](abilities.md)
32 | - [导入和别名 (Uses and Aliases)](uses.md)
33 | - [友元函数 (Friends)](friends.md)
34 | - [程序包 (Packages)](packages.md)
35 | - [单元测试 (Unit Tests)](unit-testing.md)
36 |
37 | ## Global Storage
38 |
39 | - [全局存储结构 (Global Storage Structure)](global-storage-structure.md)
40 | - [全局存储操作 (Global Storage Operators)](global-storage-operators.md)
41 |
42 | ## Reference
43 |
44 | - [标准库 (Standard Library)](standard-library.md)
45 | - [Move 编码规范 (Coding Conventions)](coding-conventions.md)
46 |
--------------------------------------------------------------------------------
/src/abilities.md:
--------------------------------------------------------------------------------
1 | # 能力 (abilities)
2 |
3 | Abilities are a typing feature in Move that control what actions are permissible for values of a given type. This system grants fine grained control over the "linear" typing behavior of values, as well as if and how values are used in global storage. This is implemented by gating access to certain bytecode instructions so that for a value to be used with the bytecode instruction, it must have the ability required (if one is required at all—not every instruction is gated by an ability).
4 |
5 | 能力是 Move 语言中的一种类型特性,用于控制对给定类型的值允许哪些操作。 该系统对值的“线性”类型行为以及值如何在全局存储中使用提供细粒度控制。这是通过对某些字节码指令的进行访问控制来实现的,因此对于要与字节码指令一起使用的值,它必须具有所需的能力(如果需要的话,并非每条指令都由能力控制)
6 |
7 | ## 四种能力 (The Four Abilities)
8 |
9 | The four abilities are:
10 |
11 | * [`copy`](#copy)
12 | * Allows values of types with this ability to be copied.
13 | * [`drop`](#drop)
14 | * Allows values of types with this ability to be popped/dropped.
15 | * [`store`](#store)
16 | * Allows values of types with this ability to exist inside a struct in global storage.
17 | * [`key`](#key)
18 | * Allows the type to serve as a key for global storage operations.
19 |
20 | 这四种能力分别是:
21 |
22 | * [`copy`](#copy) 复制
23 | * 允许此类型的值被复制
24 |
25 | * [`drop`](#drop) 丢弃
26 | * 允许此类型的值被弹出/丢弃
27 |
28 | * [`store`](#store) 存储
29 | * 允许此类型的值存在于全局存储的某个结构体中
30 |
31 | * [`key`](#key) 键值
32 | * 允许此类型作为全局存储中的键(具有 `key` 能力的类型才能保存到全局存储中)
33 |
34 |
35 | ### `copy`
36 |
37 | The `copy` ability allows values of types with that ability to be copied. It gates the ability to copy values out of local variables with the [`copy`](./variables.md#move-and-copy) operator and to copy values via references with [dereference `*e`](./references.md#reading-and-writing-through-references).
38 |
39 | If a value has `copy`, all values contained inside of that value have `copy`.
40 |
41 | `copy` 能力允许具有此能力的类型的值被复制。 它限制了从本地变量通过 [`copy`](./variables.md#.move-and-copy)能力复制值以及通过 [`dereference *e`](./references.html#reading-and-writing-through-references)复制值这两种情况之外的复制操作。
42 |
43 | 如果一个值具有 `copy` 能力,那么这个值内部的所有值都有 `copy` 能力。
44 |
45 | ### `drop`
46 |
47 | The `drop` ability allows values of types with that ability to be dropped. By dropped, we mean that value is not transferred and is effectively destroyed as the Move program executes. As such, this ability gates the ability to ignore values in a multitude of locations, including:
48 | * not using the value in a local variable or parameter
49 | * not using the value in a [sequence via `;`](./variables.md#expression-blocks)
50 | * overwriting values in variables in [assignments](./variables.md#assignments)
51 | * overwriting values via references when [writing `*e1 = e2`](./references.md#reading-and-writing-through-references).
52 |
53 | If a value has `drop`, all values contained inside of that value have `drop`.
54 |
55 | `drop` 能力允许类型的值被丢弃。丢弃的意思程序执行后值会被有效的销毁而不必被转移。因此,这个能力限制在多个位置忽略使用值的可能性,包括:
56 | * 未被使用的局部变量或者参数
57 | * 未被使用的 [`sequence` via `;`](./variables.md#expression-blocks)中的值
58 | * 覆盖[赋值(assignments)](./variables.html#assignments)变量中的值
59 | * [写入(writing) `*e1 = e2`](https://move-language.github.io/move/references.html#reading-and-writing-through-references) 时通过引用覆盖的值。
60 |
61 | 如果一个值具有 `drop` 能力,那么这个值内部的所有值都有 `drop` 能力。
62 |
63 | ### `store`
64 |
65 | The `store` ability allows values of types with this ability to exist inside of a struct (resource) in global storage, *but* not necessarily as a top-level resource in global storage. This is the only ability that does not directly gate an operation. Instead it gates the existence in global storage when used in tandem with `key`.
66 |
67 | If a value has `store`, all values contained inside of that value have `store`
68 |
69 | `store` 能力允许具有这种能力的类型的值位于[全局存储](./global-storage-operators.html)中的结构体(资源)内, *但不一定是* 全局存储中的顶级资源。这是唯一不直接限制操作的能力。相反,当(`store`)与 `key` 一起使用时,它对全局存储中的可行性进行把关。。
70 |
71 | 如果一个值具有 `store` 能力,那么这个值内部的所有值都有 `store` 能力。
72 |
73 | ### `key`
74 |
75 | The `key` ability allows the type to serve as a key for [global storage operations](./global-storage-operators.md). It gates all global storage operations, so in order for a type to be used with `move_to`, `borrow_global`, `move_from`, etc., the type must have the `key` ability. Note that the operations still must be used in the module where the `key` type is defined (in a sense, the operations are private to the defining module).
76 |
77 | If a value has `key`, all values contained inside of that value have `store`. This is the only ability with this sort of asymmetry.
78 |
79 | `key` 能力允许此类型作为[全局存储](./global-storage-operators.html)中的键。它会限制所有[全局存储](./global-storage-operators.html)中的操作,因此一个类型如果与 `move_to`, `borrow_global`, `move_from` 等一起使用,那么这个类型必须具备 `key` 能力。请注意,这些操作仍然必须在定义 `key` 类型的模块中使用(从某种意义上说,这些操作是此模块的私有操作)。
80 |
81 | 如果有一个值有 `key` 能力,那么这个值包含的所有字段值也都具有 `store` 能力,`key` 能力是唯一一个具有非对称的能力。
82 |
83 | ## Builtin Types (内置类型)
84 |
85 |
86 | Most primitive, builtin types have `copy`, `drop`, and `store` with the exception of `signer`, which just has `store`
87 |
88 | * `bool`, `u8`, `u64`, `u128`, and `address` all have `copy`, `drop`, and `store`.
89 | * `signer` has `drop`
90 | * Cannot be copied and cannot be put into global storage
91 | * `vector` may have `copy`, `drop`, and `store` depending on the abilities of `T`.
92 | * See [Conditional Abilities and Generic Types](#conditional-abilities-and-generic-types) for more details.
93 | * Immutable references `&` and mutable references `&mut` both have `copy` and `drop`.
94 | * This refers to copying and dropping the reference itself, not what they refer to.
95 | * References cannot appear in global storage, hence they do not have `store`.
96 |
97 | None of the primitive types have `key`, meaning none of them can be used directly with the [global storage operations](./global-storage-operators.md).
98 |
99 | 几乎所有内置的基本类型具都有 `copy`,`drop`,以及 `store` 能力,`singer` 除外,它只有 `drop` 能力(原文是 `store` 有误,译者注)
100 |
101 | * `bool`, `u8`, `u64`, `u128`, `address` 都具有 `copy`, `drop`, 以及 `store` 能力。
102 | * `signer` 具有 `drop` 能力。 不能被复制以及不能被存放在全局存储中
103 | * `vector` 可能具有 `copy`,`drop`,以及`store` 能力,这依赖于 `T` 具有的能力。 查看 [条件能力与泛型类型](#conditional-abilities-and-generic-types)获取详情
104 | * 不可变引用 `&` 和可变引用 `&mut` 都具有 `copy` 和 `drop` 能力。
105 | * 这是指复制和删除引用本身,而不是它们所引用的内容。
106 | * 引用不能出现在全局存储中,因此它们没有 `store` 能力。
107 |
108 | 所有基本类型都没有 `key`,这意味着它们都不能直接用于[全局存储操作](./global-storage-operators.html)。
109 |
110 | ## Annotating Structs (标注结构体)
111 |
112 | To declare that a `struct` has an ability, it is declared with `has ` after the struct name but before the fields. For example:
113 |
114 | 要声明一个 `struct` 具有某个能力,它在结构体名称之后, 在字段之前用 `has ` 声明。例如:
115 |
116 | ```move
117 | struct Ignorable has drop { f: u64 }
118 | struct Pair has copy, drop, store { x: u64, y: u64 }
119 | ```
120 |
121 | In this case: `Ignorable` has the `drop` ability. `Pair` has `copy`, `drop`, and `store`.
122 |
123 | 在这个例子中:`Ignorable` 具有 `drop` 能力。 `Pair` 具有 `copy`、`drop` 和 `store` 能力。
124 |
125 | All of these abilities have strong guarantees over these gated operations. The operation can be performed on the value only if it has that ability; even if the value is deeply nested inside of some other collection!
126 |
127 | 所有这些能力对这些访问操作都有强有力的保证。只有具有该能力,才能对值执行对应的操作;即使该值深层嵌套在其他集合中!
128 |
129 | As such: when declaring a struct’s abilities, certain requirements are placed on the fields. All fields must satisfy these constraints. These rules are necessary so that structs satisfy the reachability rules for the abilities given above. If a struct is declared with the ability...
130 |
131 | * `copy`, all fields must have `copy`.
132 | * `drop`, all fields must have `drop`.
133 | * `store`, all fields must have `store`.
134 | * `key`, all fields must have `store`.
135 | * `key` is the only ability currently that doesn’t require itself.
136 |
137 | 因此:在声明结构体的能力时,对字段提出了某些要求。所有字段都必须满足这些约束。这些规则是必要的,以便结构体满足上述功能的可达性规则。如果一个结构被声明为具有某能力:
138 |
139 | * `copy`, 所有的字段必须具有 `copy` 能力。
140 | * `drop`,所有的字段必须具有 `drop` 能力。
141 | * `store`,所有的字段必须具有 `store` 能力。
142 | * `key`,所有的字段必须具有 `store` 能力。`key` 是目前唯一不需要包含自身的能力。
143 |
144 | 例如:
145 |
146 | ```move
147 | // A struct without any abilities
148 | struct NoAbilities {}
149 |
150 | struct WantsCopy has copy {
151 | f: NoAbilities, // ERROR 'NoAbilities' does not have 'copy'
152 | }
153 | ```
154 |
155 | and similarly:
156 |
157 | 类似的:
158 |
159 | ```move
160 | // A struct without any abilities
161 | struct NoAbilities {}
162 |
163 | struct MyResource has key {
164 | f: NoAbilities, // Error 'NoAbilities' does not have 'store'
165 | }
166 | ```
167 |
168 | ## Conditional Abilities and Generic Types (条件能力与泛型类型)
169 |
170 | When abilities are annotated on a generic type, not all instances of that type are guaranteed to have that ability. Consider this struct declaration:
171 |
172 | 在泛型类型上标注能力时,并非该类型的所有实例都保证具有该能力。考虑这个结构体声明:
173 |
174 | ```move
175 | struct Cup has copy, drop, store, key { item: T }
176 | ```
177 |
178 |
179 | It might be very helpful if `Cup` could hold any type, regardless of its abilities. The type system can *see* the type parameter, so it should be able to remove abilities from `Cup` if it *sees* a type parameter that would violate the guarantees for that ability.
180 |
181 | This behavior might sound a bit confusing at first, but it might be more understandable if we think about collection types. We could consider the builtin type `vector` to have the following type declaration:
182 |
183 | 如果 `Cup` 可以容纳任何类型,可能会很有帮助,不管它的能力如何。类型系统可以 *看到* 类型参数,因此,如果它 *发现* 一个类型参数违反了对该能力的保证,它应该能够从 `Cup` 中删除能力。
184 |
185 | 这种行为一开始可能听起来有点令人困惑,但如果我们考虑一下集合类型,它可能会更容易理解。我们可以认为内置类型 `Vector` 具有以下类型声明:
186 |
187 | ```move
188 | vector has copy, drop, store;
189 | ```
190 |
191 | We want `vector`s to work with any type. We don't want separate `vector` types for different abilities. So what are the rules we would want? Precisely the same that we would want with the field rules above. So, it would be safe to copy a `vector` value only if the inner elements can be copied. It would be safe to ignore a `vector` value only if the inner elements can be ignored/dropped. And, it would be safe to put a `vector` in global storage only if the inner elements can be in global storage.
192 |
193 | To have this extra expressiveness, a type might not have all the abilities it was declared with depending on the instantiation of that type; instead, the abilities a type will have depends on both its declaration **and** its type arguments. For any type, type parameters are pessimistically assumed to be used inside of the struct, so the abilities are only granted if the type parameters meet the requirements described above for fields. Taking `Cup` from above as an example:
194 |
195 | * `Cup` has the ability `copy` only if `T` has `copy`.
196 | * It has `drop` only if `T` has `drop`.
197 | * It has `store` only if `T` has `store`.
198 | * It has `key` only if `T` has `store`.
199 |
200 | 我们希望 `vector` 适用于任何类型。我们不希望针对不同的能力使用不同的 `vector` 类型。那么我们想要的规则是什么?与上面的字段规则完全相同。因此,仅当可以复制内部元素时,复制`vector` 值才是安全的。仅当可以忽略/丢弃内部元素时,忽略 `vector` 值才是安全的。而且,仅当内部元素可以在全局存储中时,将向量放入全局存储中才是安全的。
201 |
202 | 为了具有这种额外的表现力,一个类型可能不具备它声明的所有能力,具体取决于该类型的实例化;相反,一个类型的能力取决于它的声明 **和** 它的类型参数。对于任何类型,类型参数都被悲观地假定为在结构体内部使用,因此只有在类型参数满足上述字段要求时才授予这些能力。以上面的 `Cup` 为例:
203 |
204 | * `Cup` 拥有 `copy` 能力 仅当 `T` 拥有 `copy` 能力时。
205 | * `Cup` 拥有 `drop` 能力 仅当 `T` 拥有 `drop` 能力时。
206 | * `Cup` 拥有 `store` 能力 仅当 `T` 拥有 `store` 能力时。
207 | * `Cup` 拥有 `key` 能力 仅当 `T` 拥有 `store` 能力时。
208 |
209 | Here are examples for this conditional system for each ability:
210 |
211 | 以下是每个能力的条件系统的示例:
212 |
213 | ### Example: conditional `copy`
214 |
215 | ```move
216 | struct NoAbilities {}
217 | struct S has copy, drop { f: bool }
218 | struct Cup has copy, drop, store { item: T }
219 |
220 | fun example(c_x: Cup, c_s: Cup) {
221 | // Valid, 'Cup' has 'copy' because 'u64' has 'copy'
222 | let c_x2 = copy c_x;
223 | // Valid, 'Cup' has 'copy' because 'S' has 'copy'
224 | let c_s2 = copy c_s;
225 | }
226 |
227 | fun invalid(c_account: Cup, c_n: Cup) {
228 | // Invalid, 'Cup' does not have 'copy'.
229 | // Even though 'Cup' was declared with copy, the instance does not have 'copy'
230 | // because 'signer' does not have 'copy'
231 | let c_account2 = copy c_account;
232 | // Invalid, 'Cup' does not have 'copy'
233 | // because 'NoAbilities' does not have 'copy'
234 | let c_n2 = copy c_n;
235 | }
236 | ```
237 |
238 | ### Example: conditional `drop`
239 |
240 | ```move
241 | struct NoAbilities {}
242 | struct S has copy, drop { f: bool }
243 | struct Cup has copy, drop, store { item: T }
244 |
245 | fun unused() {
246 | Cup { item: true }; // Valid, 'Cup' has 'drop'
247 | Cup { item: S { f: false }}; // Valid, 'Cup' has 'drop'
248 | }
249 |
250 | fun left_in_local(c_account: Cup): u64 {
251 | let c_b = Cup { item: true };
252 | let c_s = Cup { item: S { f: false }};
253 | // Valid return: 'c_account', 'c_b', and 'c_s' have values
254 | // but 'Cup', 'Cup', and 'Cup' have 'drop'
255 | 0
256 | }
257 |
258 | fun invalid_unused() {
259 | // Invalid, Cannot ignore 'Cup' because it does not have 'drop'.
260 | // Even though 'Cup' was declared with 'drop', the instance does not have 'drop'
261 | // because 'NoAbilities' does not have 'drop'
262 | Cup { item: NoAbilities {}};
263 | }
264 |
265 | fun invalid_left_in_local(): u64 {
266 | let n = Cup { item: NoAbilities {}};
267 | // Invalid return: 'c_n' has a value
268 | // and 'Cup' does not have 'drop'
269 | 0
270 | }
271 | ```
272 |
273 | ### Example: conditional `store`
274 |
275 | ```move
276 | struct Cup has copy, drop, store { item: T }
277 |
278 | // 'MyInnerResource' is declared with 'store' so all fields need 'store'
279 | struct MyInnerResource has store {
280 | yes: Cup, // Valid, 'Cup' has 'store'
281 | // no: Cup, Invalid, 'Cup' does not have 'store'
282 | }
283 |
284 | // 'MyResource' is declared with 'key' so all fields need 'store'
285 | struct MyResource has key {
286 | yes: Cup, // Valid, 'Cup' has 'store'
287 | inner: Cup, // Valid, 'Cup' has 'store'
288 | // no: Cup, Invalid, 'Cup' does not have 'store'
289 | }
290 | ```
291 |
292 | ### Example: conditional `key`
293 |
294 | ```move
295 | struct NoAbilities {}
296 | struct MyResource has key { f: T }
297 |
298 | fun valid(account: &signer) acquires MyResource {
299 | let addr = signer::address_of(account);
300 | // Valid, 'MyResource' has 'key'
301 | let has_resource = exists>(addr);
302 | if (!has_resource) {
303 | // Valid, 'MyResource' has 'key'
304 | move_to(account, MyResource { f: 0 })
305 | };
306 | // Valid, 'MyResource' has 'key'
307 | let r = borrow_global_mut>(addr)
308 | r.f = r.f + 1;
309 | }
310 |
311 | fun invalid(account: &signer) {
312 | // Invalid, 'MyResource' does not have 'key'
313 | let has_it = exists>(addr);
314 | // Invalid, 'MyResource' does not have 'key'
315 | let NoAbilities {} = move_from(addr);
316 | // Invalid, 'MyResource' does not have 'key'
317 | move_to(account, NoAbilities {});
318 | // Invalid, 'MyResource' does not have 'key'
319 | borrow_global(addr);
320 | }
321 | ```
322 |
--------------------------------------------------------------------------------
/src/abort-and-assert.md:
--------------------------------------------------------------------------------
1 | # 中止和断言 (Abort and Assert)
2 |
3 | [`return`](./functions.md) and `abort` are two control flow constructs that end execution, one for
4 | the current function and one for the entire transaction.
5 |
6 | More information on [`return` can be found in the linked section](./functions.md)
7 |
8 | [`return`](./functions.md) 和 `abort` 是两种结束程序执行的控制流结构。前者针对当前函数,后者针对整个事务。
9 |
10 | [`return`](./functions.md)的更多信息可以参考链接中的文章。
11 |
12 | ## `abort` 中止
13 |
14 | `abort` is an expression that takes one argument: an **abort code** of type `u64`. For example:
15 |
16 | `abort` 表达式只接受一个参数: 类型为 `u64` 的**中止代码**。例如:
17 |
18 | ```move
19 | abort 42
20 | ```
21 |
22 | The `abort` expression halts execution the current function and reverts all changes made to global
23 | state by the current transaction. There is no mechanism for "catching" or otherwise handling an `abort`.
24 |
25 | `abort` 表达式会中止执行当前函数并恢复当前事务对全局状态所做的所有更改。Move语言没有“捉捕”或者额外处理`abort`的机制。
26 |
27 | Luckily, in Move transactions are all or nothing, meaning any changes to global storage are made all
28 | at once only if the transaction succeeds. Because of this transactional commitment of changes, after
29 | an abort there is no need to worry about backing out changes. While this approach is lacking in
30 | flexibility, it is incredibly simple and predictable.
31 |
32 | 幸运的是,在Move里事务的计算要么完全执行要么完全不执行。这意味着只有在事务成功时,任何对全局存储状态的改变才会被一并执行。
33 | 由于这种对于所有更改的事务承诺,在 `abort` 之后我们不需要担心去回滚任何更改。尽管这种方法缺少灵活性,它还是非常简单和可预测的。
34 |
35 |
36 | Similar to [`return`](./functions.md), `abort` is useful for exiting control flow when some condition cannot be met.
37 |
38 | In this example, the function will pop two items off of the vector, but will abort early if the vector does not have two items
39 |
40 | 与 [`return`](./functions.md)相似, 在一些条件无法被满足的时候,`abort` 可以被用于退出控制流(control flow)。
41 |
42 | 在以下示例中,目标函数会从vector里弹出两个元素,但是如果vector中并没有两个元素,函数会提前中止。
43 |
44 | ```move=
45 | use std::vector;
46 | fun pop_twice(v: &mut vector): (T, T) {
47 | if (vector::length(v) < 2) abort 42;
48 |
49 | (vector::pop_back(v), vector::pop_back(v))
50 | }
51 | ```
52 |
53 | This is even more useful deep inside a control-flow construct. For example, this function checks
54 | that all numbers in the vector are less than the specified `bound`. And aborts otherwise
55 |
56 | 这在控制流结构的深处甚至会更有用。例如,此函数检查vector中是否所有数字都小于指定的边界(`bound`)。否则函数中止:
57 |
58 | ```move=
59 | use std::vector;
60 | fun check_vec(v: &vector, bound: u64) {
61 | let i = 0;
62 | let n = vector::length(v);
63 | while (i < n) {
64 | let cur = *vector::borrow(v, i);
65 | if (cur > bound) abort 42;
66 | i = i + 1;
67 | }
68 | }
69 | ```
70 |
71 | ### `assert` 断言
72 |
73 | `assert` is a builtin, macro-like operation provided by the Move compiler. It takes two arguments, a condition of type `bool` and a code of type `u64`
74 |
75 | `assert` 是 Move 编译器提供的内置的类宏(macro-like)操作。它需要两个参数:一个 `bool` 类型的条件和一个 `u64` 类型的错误状态码(类似HTTP中的StatusCode: 404, 500等,译者注)
76 |
77 | ```move
78 | assert!(condition: bool, code: u64)
79 | ```
80 |
81 | Since the operation is a macro, it must be invoked with the `!`. This is to convey that the
82 | arguments to `assert` are call-by-expression. In other words, `assert` is not a normal function and
83 | does not exist at the bytecode level. It is replaced inside the compiler with
84 |
85 | 由于该操作是一个宏,因此必须使用 `!` 调用它。这是为了表达 `assert` 的参数属于表达式调用(call-by-expression)。换句话说,`assert` 不是一个正常的函数,并且在字节码(bytecode)级别不存在。它在编译器内部被替换为以下代码:
86 |
87 | ```move
88 | if (condition) () else abort code
89 | ```
90 |
91 | `assert` is more commonly used than just `abort` by itself. The `abort` examples above can be rewritten using `assert`
92 |
93 | `assert` 比 `abort` 本身更常用。上面的 `abort` 示例可以使用 `assert` 重写
94 |
95 | ```move=
96 | use std::vector;
97 | fun pop_twice(v: &mut vector): (T, T) {
98 | assert!(vector::length(v) >= 2, 42); // 现在使用'assert'
99 |
100 | (vector::pop_back(v), vector::pop_back(v))
101 | }
102 | ```
103 |
104 | 和
105 |
106 | ```move=
107 | use std::vector;
108 | fun check_vec(v: &vector, bound: u64) {
109 | let i = 0;
110 | let n = vector::length(v);
111 | while (i < n) {
112 | let cur = *vector::borrow(v, i);
113 | assert!(cur <= bound, 42); // 现在使用 'assert'
114 | i = i + 1;
115 | }
116 | }
117 | ```
118 |
119 | Note that because the operation is replaced with this `if-else`, the argument for the `code` is not
120 | always evaluated. For example:
121 |
122 | 请注意,因为此操作被替换为 `if-else`,这段 `代码` 的参数不是总是被执行(evaluated)。例如:
123 |
124 | ```move
125 | assert!(true, 1 / 0)
126 | ```
127 |
128 | Will not result in an arithmetic error, it is equivalent to
129 |
130 | 不会导致算术错误,因为它相当于:
131 |
132 | ```move
133 | if (true) () else (1 / 0)
134 | ```
135 |
136 | So the arithmetic expression is never evaluated!
137 |
138 | 所以这个算术表达式永远不会被执行(evaluated)!
139 |
140 | ### Abort codes in the Move VM (Move虚拟机中的中止代码)
141 |
142 | When using `abort`, it is important to understand how the `u64` code will be used by the VM.
143 |
144 | Normally, after successful execution, the Move VM produces a change-set for the changes made to
145 | global storage (added/removed resources, updates to existing resources, etc).
146 |
147 | 当使用 `abort` 时,理解虚拟机将如何使用 `u64` 代码是非常重要的。
148 |
149 | 通常,在成功执行后,Move 虚拟机会为对全局存储(添加/删除资源、更新现有资源等)所做的更改生成一个更改集。
150 |
151 | If an `abort` is reached, the VM will instead indicate an error. Included in that error will be two
152 | pieces of information:
153 |
154 | - The module that produced the abort (address and name)
155 | - The abort code.
156 |
157 | For example
158 |
159 | 如果执行到 `abort` 代码,虚拟机将指示错误。该错误中包含两块信息:
160 |
161 | - 发生中止的模块(地址和名称)
162 | - 错误状态码。
163 |
164 | 例如
165 |
166 | ```move=
167 | address 0x2 {
168 | module example {
169 | public fun aborts() {
170 | abort 42
171 | }
172 | }
173 | }
174 |
175 | script {
176 | fun always_aborts() {
177 | 0x2::example::aborts()
178 | }
179 | }
180 | ```
181 |
182 | If a transaction, such as the script `always_aborts` above, calls `0x2::example::aborts`, the VM
183 | would produce an error that indicated the module `0x2::example` and the code `42`.
184 |
185 | This can be useful for having multiple aborts being grouped together inside a module.
186 |
187 | In this example, the module has two separate error codes used in multiple functions
188 |
189 | 如果一个事务,例如上面的脚本 `always_aborts` 调用了 `0x2::example::aborts`,虚拟机将产生一个指示模块 `0x2::example` 和错误状态码 `42` 的错误。
190 |
191 | 这在一个模块内将多个中止功能组合起来会很有用。
192 |
193 | 在以下示例中,模块有两个单独的错误状态码,用于多个函数
194 |
195 | ```move=
196 | address 0x42 {
197 | module example {
198 |
199 | use std::vector;
200 |
201 | const EMPTY_VECTOR: u64 = 0;
202 | const INDEX_OUT_OF_BOUNDS: u64 = 1;
203 |
204 | // 移动 i 到 j, 移动 j 到 k, 移动 k 到 i
205 | public fun rotate_three(v: &mut vector, i: u64, j: u64, k: u64) {
206 | let n = vector::length(v);
207 | assert!(n > 0, EMPTY_VECTOR);
208 | assert!(i < n, INDEX_OUT_OF_BOUNDS);
209 | assert!(j < n, INDEX_OUT_OF_BOUNDS);
210 | assert!(k < n, INDEX_OUT_OF_BOUNDS);
211 |
212 | vector::swap(v, i, k);
213 | vector::swap(v, j, k);
214 | }
215 |
216 | public fun remove_twice(v: &mut vector, i: u64, j: u64): (T, T) {
217 | let n = vector::length(v);
218 | assert!(n > 0, EMPTY_VECTOR);
219 | assert!(i < n, INDEX_OUT_OF_BOUNDS);
220 | assert!(j < n, INDEX_OUT_OF_BOUNDS);
221 | assert!(i > j, INDEX_OUT_OF_BOUNDS);
222 |
223 | (vector::remove(v, i), vector::remove(v, j))
224 | }
225 | }
226 | }
227 | ```
228 |
229 | ## The type of `abort` (`abort` 的类型)
230 |
231 | The `abort i` expression can have any type! This is because both constructs break from the normal
232 | control flow, so they never need to evaluate to the value of that type.
233 |
234 | The following are not useful, but they will type check
235 |
236 | `abort i` 表达式可以有任何类型!这是因为这两种构造都打破了正常控制流,因此他们永远不需要计算该类型的值。
237 |
238 | 以下的示例不是特别有用,但它们会做类型检查
239 |
240 | ```move
241 | let y: address = abort 0;
242 | ```
243 |
244 | This behavior can be helpful in situations where you have a branching instruction that produces a
245 | value on some branches, but not all. For example:
246 |
247 | 在您有一个分支指令,并且这个指令会产生某些分支(不是全部)的值的时候,这种行为会非常有用。例如:
248 |
249 | ```move
250 | let b =
251 | if (x == 0) false
252 | else if (x == 1) true
253 | else abort 42;
254 | // ^^^^^^^^ `abort 42` 的类型是 `bool`
255 | ```
256 |
--------------------------------------------------------------------------------
/src/address.md:
--------------------------------------------------------------------------------
1 | # 地址(Address)
2 |
3 | `address` is a built-in type in Move that is used to represent locations (sometimes called accounts) in global storage. An `address` value is a 128-bit (16 byte) identifier. At a given address, two things can be stored: [Modules](./modules-and-scripts.md) and [Resources](./structs-and-resources.md).
4 |
5 | Although an `address` is a 128 bit integer under the hood, Move addresses are intentionally opaque---they cannot be created from integers, they do not support arithmetic operations, and they cannot be modified. Even though there might be interesting programs that would use such a feature (e.g., pointer arithmetic in C fills a similar niche), Move does not allow this dynamic behavior because it has been designed from the ground up to support static verification.
6 |
7 | You can use runtime address values (values of type `address`) to access resources at that address. You *cannot* access modules at runtime via address values.
8 |
9 | `地址(address)`是 Move 中的内置类型,用于表示全局存储中的的位置(有时称为账户)。`地址(address)` 值是一个 128 位(16 字节)标识符。在一个给定的地址,可以存储两样东西:[模块(Module)](./modules-and-scripts.md)和[资源(Resources)](./structs-and-resources.md)。
10 |
11 | 虽然`地址(address)`在底层是一个 128 位整数,但 Move 语言有意让其不透明 —— 它们不能从整数创建,不支持算术运算,也不能修改。即使可能有一些有趣的程序会使用这种特性(例如,C 中的指针算法实现了类似壁龛(niche)的功能),但 Move 语言不允许这种动态行为,因为它从头开始就被设计为支持静态验证。*(壁龛指安装在墙壁上的小格子或在墙身上留出的作为贮藏设施的空间,最早在宗教上是指排放佛像的小空间,现在多用在家庭装修上,因其不占建筑面积,使用比较方便,深受大家喜爱,Joe 注)*
12 |
13 | 你可以通过运行时地址值(`address` 类型的值)来访问该地址处的资源。但*无法*在运行时通过地址值访问模块。
14 |
15 | ## 地址及其语法(Addresses and Their Syntax)
16 |
17 | Addresses come in two flavors, named or numerical. The syntax for a named address follows the
18 | same rules for any named identifier in Move. The syntax of a numerical address is not restricted
19 | to hex-encoded values, and any valid [`u128` numerical value](./integers.md) can be used as an
20 | address value, e.g., `42`, `0xCAFE`, and `2021` are all valid numerical address
21 | literals.
22 |
23 | 地址有两种形式:*命名的*或*数值的*。命名地址的语法遵循 Move 命名标识符的规则。数值地址的语法不受十六进制编码值的限制,任何有效的 [`u128` 数值](./integers.md)都可以用作地址值。例如,`42`,`0xCFAE` 和 `2021` 都是合法有效的数值地址字面量(literal)。
24 |
25 | To distinguish when an address is being used in an expression context or not, the
26 | syntax when using an address differs depending on the context where it's used:
27 | * When an address is used as an expression the address must be prefixed by the `@` character, i.e., [`@`](./integers.md) or `@`.
28 | * Outside of expression contexts, the address may be written without the leading `@` character, i.e., [``](./integers.md) or ``.
29 |
30 |
31 | 为了区分何时在表达式上下文中使用地址,使用地址时的语法根据使用地址的上下文而有所不同:
32 |
33 | * 当地址被用作表达式时,地址必须以 `@` 字符为前缀,例如:[`@`](./integers.md) 或 `@`。
34 | * 在表达式上下文之外,地址可以不带前缀字符 `@`。例如:[``](./integers.md) 或 ``。
35 |
36 | In general, you can think of `@` as an operator that takes an address from being a namespace item to being an expression item.
37 |
38 | 通常,可以将 `@` 视为将地址从命名空间项变为表达式项的运算符。
39 |
40 | ## 命名地址(Named Addresses)
41 |
42 | Named addresses are a feature that allow identifiers to be used in place of
43 | numerical values in any spot where addresses are used, and not just at the
44 | value level. Named addresses are declared and bound as top level elements
45 | (outside of modules and scripts) in Move Packages, or passed as arguments
46 | to the Move compiler.
47 |
48 | 命名地址是一项特性,它允许在使用地址的任何地方使用标识符代替数值,而不仅仅是在值级别。命名地址被声明并绑定为 Move 包中的顶级元素(模块和脚本之外)或作为参数传递给 Move 编译器。
49 |
50 | Named addresses only exist at the source language level and will be fully
51 | substituted for their value at the bytecode level. Because of this, modules
52 | and module members _must_ be accessed through the module's named address
53 | and not through the numerical value assigned to the named address during
54 | compilation, e.g., `use my_addr::foo` is _not_ equivalent to `use 0x2::foo`
55 | even if the Move program is compiled with `my_addr` set to `0x2`. This
56 | distinction is discussed in more detail in the section on [Modules and
57 | Scripts](./modules-and-scripts.md).
58 |
59 | 命名地址仅存在于源语言级别,并将在字节码级别完全替代它们的值。因此,模块和模块成员*必须*通过模块的命名地址而不是编译期间分配给命名地址的数值来访问,例如:`use my_addr::foo` *不等于* `use 0x2::foo`,即使 Move 程序编译时将 `my_addr` 设置成 `0x2`。这个区别在[模块和脚本](./modules-and-scripts.md)一节中有更详细的讨论。
60 |
61 | ### 例子(Examples)
62 |
63 | ```move
64 | let a1: address = @0x1; // 0x00000000000000000000000000000001 的缩写
65 | let a2: address = @0x42; // 0x00000000000000000000000000000042 的缩写
66 | let a3: address = @0xDEADBEEF; // 0x000000000000000000000000DEADBEEF 的缩写
67 | let a4: address = @0x0000000000000000000000000000000A;
68 | let a5: address = @std; // 将命名地址 `std` 的值赋给 `a5`
69 | let a6: address = @66;
70 | let a7: address = @0x42;
71 |
72 | module 66::some_module { // 不在表达式上下文中,所以不需要 @
73 | use 0x1::other_module; // 不在表达式上下文中,所以不需要 @
74 | use std::vector; // 使用其他模块时,可以使用命名地址作为命名空间项
75 | ...
76 | }
77 |
78 | module std::other_module { // 可以使用命名地址作为命名空间项来声明模块
79 | ...
80 | }
81 | ```
82 |
83 | ## 全局存储操作(Global Storage Operations)
84 |
85 | The primary purpose of `address` values are to interact with the global storage operations.
86 |
87 | `address` values are used with the `exists`, `borrow_global`, `borrow_global_mut`, and `move_from` [operations](./global-storage-operators.md).
88 |
89 | The only global storage operation that *does not* use `address` is `move_to`, which uses [`signer`](./signer.md).
90 |
91 | `address` 值主要用来与全局存储操作进行交互。
92 |
93 | `address` 值与 `exists`、`borrow_global`、`borrow_global_mut` 和 `move_from` [操作(operation)](./global-storage-operators.md)一起使用。
94 |
95 | 唯一*不使用* `address` 的全局存储操作是 `move_to`,它使用了 [`signer`](./signer.md)。
96 |
97 | ## 所有权(Ownership)
98 |
99 | As with the other scalar values built-in to the language, `address` values are implicitly copyable, meaning they can be copied without an explicit instruction such as [`copy`](./variables.md#move-and-copy).
100 |
101 | 与 Move 语言内置的其他标量值一样,`address` 值是隐式可复制的,这意味着它们可以在没有显式指令(例如 [`copy`](./variables.md#移动和复制move-and-copy))的情况下复制。
102 |
--------------------------------------------------------------------------------
/src/bool.md:
--------------------------------------------------------------------------------
1 | # 布尔类型 (Bool)
2 |
3 | `bool`is Move's primitive type for boolean `true` and `false`values.
4 |
5 | `bool` 是 Move 布尔基本类型,有 `true` 和 `false` 两个值。
6 |
7 | ## 字面量 (Literals)
8 |
9 | Literals for `bool` are either `true` or `false` .
10 |
11 | 布尔类型字面值只能是 `true` 或者 `false`中的一个 。
12 |
13 | ## 操作 (Operations)
14 |
15 | ### 逻辑运算 (Logical)
16 |
17 | `bool`supports three logical operations:
18 |
19 | | Syntax | Description | Equivalent Expression |
20 | | ------------------------- | ---------------------------- | ------------------------------------------------------------------- |
21 | | `&&` | short-circuiting logical and | `p && q` is equivalent to `if (p) q else false` |
22 | | ||
| short-circuiting logical or | p || q
is equivalent to `if (p) true else q` |
23 | | `!` | logical negation | `!p` is equivalent to `if (p) false else true` |
24 |
25 |
26 | `bool` 支持三种逻辑运算:
27 |
28 | | 句法 | 描述 | Equivalent Expression |
29 | | ------ | ---------------------------- | ----------------------------------------------- |
30 | | `&&` | 短路逻辑与(short-circuiting logical and) | `p && q` 等价于 `if (p) q else false` |
31 | | ||
| 短路逻辑或(short-circuiting logical or) | `p || q` 等价于 `if (p) true else q` |
32 | | `!` | 逻辑非(logical negation) | `!p` 等价于 `if (p) false else true` |
33 |
34 |
35 | ### 控制流 (Control Flow)
36 |
37 | `bool`values are used in several of Move's control-flow constructs:
38 |
39 | 布尔值用于 Move 的多个控制流结构中:
40 |
41 | - [`if (bool) { ... }`](./conditionals.html)
42 | - [`while (bool) { .. }`](/loops.html)
43 | - [`assert!(bool, u64)`](./abort-and-assert.html)
44 |
45 | ## 所有权 (Ownership)
46 |
47 | As with the other scalar values built-in to the language, boolean values are implicitly copyable, meaning they can be copied without an explicit instruction such as `[copy]().`
48 |
49 | 与语言内置的其他标量值一样,布尔值是隐式可复制的,这意味着它们可以在没有明确指令如[`copy`](./variables.md#move-and-copy)的情况下复制。
50 |
--------------------------------------------------------------------------------
/src/coding-conventions.md:
--------------------------------------------------------------------------------
1 | # Move 编码规范(Move Coding Conventions)
2 |
3 | This section lays out some basic coding conventions for Move that the Move team has found helpful. These are only recommendations, and you should feel free to use other formatting guidelines and conventions if you have a preference for them.
4 |
5 | 本节列出了 Move 团队认为有用的一些基本的 Move 编码约定。这些只是建议,如果你喜欢其他格式指南和约定,你可以随时使用它们。
6 |
7 | ## 命名(Naming)
8 |
9 | - **Module names**: should be lower snake case, e.g., `fixed_point32`, `vector`.
10 | - **Type names**: should be camel case if they are not a native type, e.g., `Coin`, `RoleId`.
11 | - **Function names**: should be lower snake case, e.g., `destroy_empty`.
12 | - **Constant names**: should be upper snake case, e.g., `REQUIRES_CAPABILITY`.
13 | - Generic types should be descriptive, or anti-descriptive where appropriate, e.g., `T` or `Element` for the Vector generic type parameter. Most of the time the "main" type in a module should be the same name as the module e.g., `option::Option`, `fixed_point32::FixedPoint32`.
14 | - **Module file names**: should be the same as the module name e.g., `Option.move`.
15 | - **Script file names**: should be lower snake case and should match the name of the “main” function in the script.
16 | - **Mixed file names**: If the file contains multiple modules and/or scripts, the file name should be lower snake case, where the name does not match any particular module/script inside.
17 |
18 |
19 |
20 | - **模块名称**:应该使用小写的蛇形命名法,例如:`fixed_point32`、`vector`。
21 | - **类型名称**:如果不是原生数据类型,则应使用驼峰命名法,例如:`Coin`、`RoleId`。
22 | - **函数名称**:应该使用小写的蛇形命名法,例如:`destroy_empty`。
23 | - **常量名称**:应该使用大写的蛇形命名法,例如:`REQUIRES_CAPABILITY`。
24 | - 泛型类型应该具备描述性,当然在适当的情况下也可以是反描述性的,例如:Vector 泛型类型的参数可以是 `T` 或 `Element`。大多数情况下,模块中的“主”类型命名应该与模块名相同,例如:`option::Option`,`fixed_point32::FixedPoint32`。
25 | - **模块文件名称**:应该与模块名相同,例如:`Option.move`。
26 | - **脚本文件名称**:应该使用小写的蛇形命名法,并且应该与脚本中的“主”函数名匹配。
27 | - **混合文件名称**:如果文件包含多个模块和/或脚本,文件命名应该使用小写的蛇形命名法,并且不需要与内部的任何特定模块/脚本名匹配。
28 |
29 | ## 导入(Imports)
30 |
31 | - All module `use` statements should be at the top of the module.
32 | - Functions should be imported and used fully qualified from the module in which they are declared, and not imported at the top level.
33 | - Types should be imported at the top-level. Where there are name clashes, `as` should be used to rename the type locally as appropriate.
34 |
35 | For example, if there is a module:
36 |
37 | - 所有模块的 `use` 语句都应该位于模块的顶部。
38 | - 函数应该从声明它们的模块中完全限定地导入和使用, 而不是在顶部导入。
39 | - 类型应该在顶部导入。如果存在名称冲突,应使用 `as` 在本地适当地重命名类型。
40 |
41 | 例如,如果有一个模块:
42 |
43 | ```move
44 | module 0x1::foo {
45 | struct Foo { }
46 | const CONST_FOO: u64 = 0;
47 | public fun do_foo(): Foo { Foo{} }
48 | ...
49 | }
50 | ```
51 |
52 | this would be imported and used as:
53 |
54 | 此时将被导入并使用:
55 |
56 | ```move
57 | module 0x1::bar {
58 | use 0x1::foo::{Self, Foo};
59 |
60 | public fun do_bar(x: u64): Foo {
61 | if (x == 10) {
62 | foo::do_foo()
63 | } else {
64 | abort 0
65 | }
66 | }
67 | ...
68 | }
69 | ```
70 |
71 | And, if there is a local name-clash when importing two modules:
72 |
73 | 并且,如果在导入两个模块时存在本地名称冲突:
74 |
75 | ```move
76 | module other_foo {
77 | struct Foo {}
78 | ...
79 | }
80 |
81 | module 0x1::importer {
82 | use 0x1::other_foo::Foo as OtherFoo;
83 | use 0x1::foo::Foo;
84 | ...
85 | }
86 | ```
87 |
88 | ## 注释(Comments)
89 |
90 | - Each module, struct, and public function declaration should be commented.
91 | - Move has doc comments `///`, regular single-line comments `//`, block comments `/* */`, and block doc comments `/** */`.
92 |
93 |
94 |
95 | - 每个模块、结构体和公共函数声明都应该有对应的注释。
96 | - Move 有文档注释 `///`,常规单行注释 `//`,块注释 `/* */`,和块文档注释 `/** */`。
97 |
98 | ## 格式化(Formatting)
99 |
100 | The Move team plans to write an autoformatter to enforce formatting conventions. However, in the meantime:
101 |
102 | - Four space indentation should be used except for `script` and `address` blocks whose contents should not be indented.
103 | - Lines should be broken if they are longer than 100 characters.
104 | - Structs and constants should be declared before all functions in a module.
105 |
106 |
107 |
108 | Move 团队计划编写一个自动格式化程序来执行格式化约定。然而,在此期间:
109 |
110 | - 除 `script` 和 `address` 块外,其他的内容应使用四个空格的缩进。
111 | - 每行代码,如果超过 100 个字符,应该换行。
112 | - 结构体和常量应该在模块中的所有函数之前声明。
113 |
--------------------------------------------------------------------------------
/src/conditionals.md:
--------------------------------------------------------------------------------
1 | # 条件语句 (Conditionals)
2 |
3 | An `if` expression specifies that some code should only be evaluated if a certain condition is true. For example:
4 |
5 | `if` 语句可以用来指定一块代码块,但只在判断条件(condition)为true时才会被执行。例如:
6 |
7 | ```move
8 | if (x > 5) x = x - 5
9 | ```
10 |
11 | The condition must be an expression of type `bool`.
12 |
13 | An `if` expression can optionally include an `else` clause to specify another expression to evaluate when the condition is false.
14 |
15 | 条件语句(condition)必须是 `bool` 类型的表达式。
16 |
17 | `if` 语句可选包含 `else` 子句,以指定当条件(condition)为 false 时要执行的另一个代码块。
18 |
19 | ```move
20 | if (y <= 10) y = y + 1 else y = 10
21 | ```
22 |
23 | Either the "true" branch or the "false" branch will be evaluated, but not both. Either branch can be a single expression or an expression block.
24 |
25 | The conditional expressions may produce values so that the `if` expression has a result.
26 |
27 | 无论是"true"分支还是"false"分支都会被执行,但不会同时执行.其中任何一个分支都可以是单行代码或代码块。条件表达式会产生值,所以 `if` 表达式会有一个结果。
28 |
29 | ```move
30 | let z = if (x < 100) x else 100;
31 | ```
32 |
33 | The expressions in the true and false branches must have compatible types. For example:
34 |
35 | true 和 false 分支的表达式类型必须是一致的,例如:
36 |
37 | ```move=
38 | // x和y必须是u64整型
39 | // x and y must be u64 integers
40 | let maximum: u64 = if (x > y) x else y;
41 |
42 | // 错误!分支的类型不一致
43 | // (ERROR! branches different types)
44 | let z = if (maximum < 10) 10u8 else 100u64;
45 |
46 | // 错误!分支的类型不一致,false-branch默认是()不是u64
47 | // ERROR! branches different types, as default false-branch is () not u64
48 | if (maximum >= 10) maximum;
49 | ```
50 |
51 | If the `else` clause is not specified, the false branch defaults to the unit value. The following are equivalent:
52 |
53 | 如果`else`子句未定义,false分支默认为 unit 。下面的例子是相等价的:
54 |
55 | ```move
56 | if (condition) true_branch // implied default: else ()
57 | if (condition) true_branch else ()
58 | ```
59 |
60 | Commonly, [`if` expressions](./conditionals.md) are used in conjunction with expression blocks.
61 |
62 | 一般来说, [`if` 表达式](./conditionals.md)与多个表达式块结合使用.
63 |
64 | ```move
65 | let maximum = if (x > y) x else y;
66 | if (maximum < 10) {
67 | x = x + 10;
68 | y = y + 10;
69 | } else if (x >= 10 && y >= 10) {
70 | x = x - 10;
71 | y = y - 10;
72 | }
73 | ```
74 |
75 | ## 条件语句的语法 (Grammar for Conditionals)
76 |
77 | > *if-expression* → **if (** *expression* **)** *expression* *else-clause**opt*
78 | > *else-clause* → **else** *expression*
79 |
--------------------------------------------------------------------------------
/src/constants.md:
--------------------------------------------------------------------------------
1 | # 常量 (Constants)
2 |
3 | Constants are a way of giving a name to shared, static values inside of a `module` or `script`.
4 |
5 | The constant's must be known at compilation. The constant's value is stored in the compiled module
6 | or script. And each time the constant is used, a new copy of that value is made.
7 |
8 | 常量是一种对 `module` 或 `script` 内的共享静态值进行命名的方法(类似变量,但值不变,译者注)。
9 |
10 | 常量必须在编译时知道。常量的值存储在编译模块或脚本中。每次使用该常量时,都会生成该值的新副本。
11 |
12 | ## 声明 (Declaration)
13 |
14 | Constant declarations begin with the `const` keyword, followed by a name, a type, and a value. They
15 | can exist in either a script or module
16 |
17 | 常量声明以 `const` 关键字开头,后跟名称、类型和值。他们可以存在于脚本或模块中
18 |
19 | ```text
20 | const : = ;
21 | ```
22 |
23 | 例如
24 |
25 | ```move=
26 | script {
27 |
28 | const MY_ERROR_CODE: u64 = 0;
29 |
30 | fun main(input: u64) {
31 | assert!(input > 0, MY_ERROR_CODE);
32 | }
33 |
34 | }
35 |
36 | address 0x42 {
37 | module example {
38 |
39 | const MY_ADDRESS: address = @0x42;
40 |
41 | public fun permissioned(s: &signer) {
42 | assert!(std::signer::address_of(s) == MY_ADDRESS, 0);
43 | }
44 |
45 | }
46 | }
47 | ```
48 |
49 | ## 命名 (Naming)
50 |
51 | Constants must start with a capital letter `A` to `Z`. After the first letter, constant names can
52 | contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`.
53 |
54 | 常量必须以大写字母 `A` 到 `Z` 开头。在第一个字母之后,常量名可以包含下划线 `_`、字母 `a` 到 `z`、字母 `A` 到 `Z` 或数字 `0` 到 `9`。
55 |
56 | ```move
57 | const FLAG: bool = false;
58 | const MY_ERROR_CODE: u64 = 0;
59 | const ADDRESS_42: address = @0x42;
60 | ```
61 |
62 | Even though you can use letters `a` to `z` in a constant. The
63 | [general style guidelines](./coding-conventions.md) are to use just uppercase letters `A` to `Z`,
64 | with underscores `_` between each word.
65 |
66 | 虽然你可以在常量中使用字母 `a` 到 `z`。但[通用风格指南](./coding-conventions.md) 只使用大写字母 `A` 到 `Z`,每个单词之间有下划线`_`。
67 |
68 | This naming restriction of starting with `A` to `Z` is in place to give room for future language features. It may or may not be removed later.
69 |
70 | 这种以 `A` 到 `Z` 开头的命名限制是为了给未来的语言特性留出空间。此限制未来可能会保留或删除。
71 |
72 | ## 可见性 (Visibility)
73 |
74 | `public` constants are not currently supported. `const` values can be used only in the declaring
75 | module.
76 |
77 | 当前不支持 `public` 常量。 `const` 值只能在声明的模块中使用。
78 |
79 | ## 有效表达式 (Valid Expressions)
80 |
81 | Currently, constants are limited to the primitive types `bool`, `u8`, `u64`, `u128`, `address`, and
82 | `vector`. Future support for other `vector` values (besides the "string"-style literals) will come later.
83 |
84 | 目前,常量仅限于原始类型 `bool`、`u8`、`u64`、`u128`、`address` 和`vector`。其他 `vector` 值(除了“string”风格的字面量)将在不远的将来获得支持。
85 |
86 | ### 值 (Values)
87 |
88 | Commonly, `const`s are assigned a simple value, or literal, of their type. For example
89 |
90 | 通常,`const` (常量)会被分配一个对应类型的简单值或字面量。例如
91 |
92 | ```move
93 | const MY_BOOL: bool = false;
94 | const MY_ADDRESS: address = @0x70DD;
95 | const BYTES: vector = b"hello world";
96 | const HEX_BYTES: vector = x"DEADBEEF";
97 | ```
98 |
99 | ### 复杂表达式 (Complex Expressions)
100 |
101 |
102 | In addition to literals, constants can include more complex expressions, as long as the compiler is
103 | able to reduce the expression to a value at compile time.
104 |
105 | Currently, equality operations, all boolean operations, all bitwise operations, and all arithmetic
106 | operations can be used.
107 |
108 | 除了字面量,常量还可以包含更复杂的表达式,只要编译器能够在编译时将表达式归纳(reduce)为一个值。
109 |
110 | 目前,相等运算、所有布尔运算、所有按位运算和所有算术运算可以使用。
111 |
112 | ```move
113 | const RULE: bool = true && false;
114 | const CAP: u64 = 10 * 100 + 1;
115 | const SHIFTY: u8 = {
116 | (1 << 1) * (1 << 2) * (1 << 3) * (1 << 4)
117 | };
118 | const HALF_MAX: u128 = 340282366920938463463374607431768211455 / 2;
119 | const EQUAL: bool = 1 == 1;
120 | ```
121 |
122 | If the operation would result in a runtime exception, the compiler will give an error that it is
123 | unable to generate the constant's value
124 |
125 | 如果操作会导致运行时异常,编译器会给出无法生成常量值的错误。
126 |
127 | ```move
128 | const DIV_BY_ZERO: u64 = 1 / 0; // error!
129 | const SHIFT_BY_A_LOT: u64 = 1 << 100; // error!
130 | const NEGATIVE_U64: u64 = 0 - 1; // error!
131 | ```
132 |
133 | Note that constants cannot currently refer to other constants. This feature, along with support for
134 | other expressions, will be added in the future.
135 |
136 | 请注意,常量当前不能引用其他常量。此功能会在将来和支持其他表达方式一起被补充。
137 |
--------------------------------------------------------------------------------
/src/diagrams/move_state.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/move-dao/move-book-zh/09268ba66c2e666c9bacece9c25e48fd03e30684/src/diagrams/move_state.png
--------------------------------------------------------------------------------
/src/diagrams/solidity_state.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/move-dao/move-book-zh/09268ba66c2e666c9bacece9c25e48fd03e30684/src/diagrams/solidity_state.png
--------------------------------------------------------------------------------
/src/equality.md:
--------------------------------------------------------------------------------
1 |
2 | # 等式 (Equality)
3 |
4 | Move supports two equality operations `==` and `!=`
5 |
6 | Move 支持两种等式操作: `==` 和 `!=`
7 |
8 | ## 操作 (Operations)
9 |
10 | | Syntax | Operation | Description |
11 | | ------ | --------- | --------------------------------------------------------------------------- |
12 | | `==` | equal | Returns `true` if the two operands have the same value, `false` otherwise |
13 | | `!=` | not equal | Returns `true` if the two operands have different values, `false` otherwise |
14 |
15 | | 语法 | 操作 | 描述 |
16 | | ------ | --------- | --------------------------------------------------------------------------- |
17 | | `==` | 相等 | 如果两个操作数(operands)值相同,返回 `true` , 否则返回 `false` |
18 | | `!=` | 不相等 | 如果两个操作数(operands)值不相同,返回 `true` , 否则返回 `false` |
19 |
20 | ### 类型校验 (Typing)
21 |
22 | Both the equal (`==`) and not-equal (`!=`) operations only work if both operands are the same type.
23 |
24 | 只有当左右两个操作数类型相同,相等操作 (`==`) 与不等操作 (`!=`) 才能正常使用。
25 |
26 | ```move
27 | 0 == 0; // `true`
28 | 1u128 == 2u128; // `false`
29 | b"hello" != x"00"; // `true`
30 | ```
31 |
32 | Equality and non-equality also work over user defined types!
33 |
34 | 等式与不等式也可以在用户自定义的类型下使用!
35 |
36 | ```move=
37 | address 0x42 {
38 | module example {
39 | struct S has copy, drop { f: u64, s: vector }
40 |
41 | fun always_true(): bool {
42 | let s = S { f: 0, s: b"" };
43 | // 括号不是必需的,但为了清楚起见在此示例中添加了括号
44 | (copy s) == s
45 | }
46 |
47 | fun always_false(): bool {
48 | let s = S { f: 0, s: b"" };
49 | // 括号不是必需的,但为了清楚起见在此示例中添加了括号
50 | (copy s) != s
51 | }
52 | }
53 | }
54 | ```
55 |
56 | If the operands have different types, there is a type checking error.
57 |
58 | 如果两边操作数的类型不同,则会出现类型检测错误。
59 |
60 | ```move
61 | 1u8 == 1u128; // 错误!
62 | // ^^^^^ 期望此变量的类型是 'u8'
63 | b"" != 0; // 错误!
64 | // ^ 期望此变量的类型是 'vector'
65 | ```
66 |
67 | ### 引用变量的类型校验 (Typing with references)
68 |
69 | When comparing [references](./references.md), the type of the reference (immutable or mutable) does
70 | not matter. This means that you can compare an immutable `&` reference with a mutable one `&mut` of
71 | the same underlying type.
72 |
73 | 当比较[引用变量](./references.md)时,引用的类别(不可变更的或可变更的(immutable or mutable))无关紧要。这意味着我们可以拿一个不可变更的 `&` 引用变量和另一个有相同相关类型的可变更的 `&mut ` 引用变量进行比较。
74 |
75 | ```move
76 | let i = &0;
77 | let m = &mut 1;
78 |
79 | i == m; // `false`
80 | m == i; // `false`
81 | m == m; // `true`
82 | i == i; // `true`
83 | ```
84 |
85 | The above is equivalent to applying an explicit freeze to each mutable reference where needed
86 |
87 | 在需要时,对每个可变引用使用显式冻结(explicit freeze)的结果与上述情况一致。
88 |
89 | ```move
90 | let i = &0;
91 | let m = &mut 1;
92 |
93 | i == freeze(m); // `false`
94 | freeze(m) == i; // `false`
95 | m == m; // `true`
96 | i == i; // `true`
97 | ```
98 | But again, the underlying type must be the same type
99 |
100 | 但同样的,我们需要两边操作数的类型一致
101 |
102 | ```move
103 | let i = &0;
104 | let s = &b"";
105 |
106 | i == s; // 错误!
107 | // ^ 期望此变量的类型是 '&u64'
108 | ```
109 |
110 | ## 限制 (Restrictions)
111 |
112 | Both `==` and `!=` consume the value when comparing them. As a result, the type system enforces that
113 | the type must have [`drop`](./abilities.md). Recall that without the [`drop` ability](./abilities.md),
114 | ownership must be transferred by the end of the function, and such values can only be explicitly destroyed
115 | within their declaring module. If these were used directly with either equality `==` or non-equality `!=`,
116 | the value would be destroyed which would break [`drop` ability](./abilities.md) safety guarantees!
117 |
118 | `==` 和 `!=` 会在比较不同变量的时候消耗 (consume)它们所包含的值,所以 Move 的类型系统会强制要求这些类型含有[`drop` 能力](./abilities.md)。回想一下,变量在没有[`drop` 能力](./abilities.md)时,所有权必须在函数结束前进行转移,而且这些值只能在其声明模块中被明确销毁(explicitly destroyed)。如果它们被直接使用于等式 `==` 或不等式 `!=` ,其值会被销毁并且这会打破[`drop` 能力](./abilities.md)的安全保证!
119 |
120 | ```move=
121 | address 0x42 {
122 | module example {
123 | struct Coin has store { value: u64 }
124 | fun invalid(c1: Coin, c2: Coin) {
125 | c1 == c2 // 错误!
126 | // ^^ ^^ 这些资源将会被销毁!
127 | }
128 | }
129 | }
130 | ```
131 |
132 |
133 | But, a programmer can _always_ borrow the value first instead of directly comparing the value, and
134 | reference types have the [`drop` ability](./abilities.md). For example
135 |
136 | 然而, 程序员 _总是_ 可以优先借用变量的值,而不直接比较它们的值。这样一来,引用变量的类型将会拥有[`drop` 能力](./abilities.md)。例如:
137 |
138 | ```move=
139 | address 0x42 {
140 | module example {
141 | struct Coin as store { value: u64 }
142 | fun swap_if_equal(c1: Coin, c2: Coin): (Coin, Coin) {
143 | let are_equal = &c1 == &c2; // 合规范的
144 | if (are_equal) (c2, c1) else (c1, c2)
145 | }
146 | }
147 | }
148 | ```
149 |
150 | ## 避免额外的复制 (Avoid Extra Copies)
151 |
152 | While a programmer _can_ compare any value whose type has [`drop`](./abilities.md), a programmer
153 | should often compare by reference to avoid expensive copies.
154 |
155 | 当程序员 _可以_ 比较其类型含有[`drop` 能力](./abilities.md)的任意值时,他们应该尽可能多地使用引用变量来比较,以此来避免昂贵的复制。
156 |
157 | ```move=
158 | let v1: vector = function_that_returns_vector();
159 | let v2: vector = function_that_returns_vector();
160 | assert!(copy v1 == copy v2, 42);
161 | // ^^^^ ^^^^
162 | use_two_vectors(v1, v2);
163 |
164 | let s1: Foo = function_that_returns_large_struct();
165 | let s2: Foo = function_that_returns_large_struct();
166 | assert!(copy s1 == copy s2, 42);
167 | // ^^^^ ^^^^
168 | use_two_foos(s1, s2);
169 | ```
170 |
171 | This code is perfectly acceptable (assuming `Foo` has [`drop`](./abilities.md)), just not efficient.
172 | The highlighted copies can be removed and replaced with borrows
173 |
174 | 以上代码是完全可以接受的(假设`Foo`具备[`drop`](./abilities.md)能力),但它不是最有效的写法。突出显示的副本可以删除并替换为借用。
175 |
176 | ```move=
177 | let v1: vector = function_that_returns_vector();
178 | let v2: vector = function_that_returns_vector();
179 | assert!(&v1 == &v2, 42);
180 | // ^ ^
181 | use_two_vectors(v1, v2);
182 |
183 | let s1: Foo = function_that_returns_large_struct();
184 | let s2: Foo = function_that_returns_large_struct();
185 | assert!(&s1 == &s2, 42);
186 | // ^ ^
187 | use_two_foos(s1, s2);
188 | ```
189 |
190 | The efficiency of the `==` itself remains the same, but the `copy`s are removed and thus the program is more efficient.
191 |
192 | `==` 本身的效率还是和之前一样,但是 `copy` 操作被移除后整个程序会比之前更有效率。
193 |
--------------------------------------------------------------------------------
/src/friends.md:
--------------------------------------------------------------------------------
1 | # 友元函数(Friends)
2 |
3 | The `friend` syntax is used to declare modules that are trusted by the current module.
4 | A trusted module is allowed to call any function defined in the current module that have the `public(friend)` visibility.
5 | For details on function visibilities, please refer to the *Visibility* section in [Functions](./functions.md).
6 |
7 | 友元语法用于声明当前模块信任的其它模块。受信任的模块可以调用当前模块中定义的任何具有`公开(友元)`可见性的函数。有关函数可见性的详细信息,请参阅[函数](./functions.md)中的可见性部分。
8 |
9 | ## 友元声明(Friend declaration)
10 |
11 | A module can declare other modules as friends via friend declaration statements, in the format of
12 |
13 | 一个模块可以通过友元声明语句将其他模块声明为友元,格式为:
14 |
15 | - `friend ` — friend declaration using fully qualified module name like the example below, or
16 | - `friend —` 使用完全合格的模块名称的友元声明,如下例所示,或
17 |
18 | ```
19 | address 0x42 {
20 | module a {
21 | friend 0x42::b;
22 | }
23 | }
24 | ```
25 |
26 | - `friend ` — friend declaration using a module name alias, where the module alias is introduced via the `use` statement.
27 | - `friend —` 使用模块名称别名的友元声明,其中模块别名是通过use语句引入的。
28 |
29 | ```move
30 | address 0x42 {
31 | module a {
32 | use 0x42::b;
33 | friend b;
34 | }
35 | }
36 | ```
37 |
38 | A module may have multiple friend declarations, and the union of all the friend modules forms the friend list.
39 | In the example below, both `0x42::B` and `0x42::C` are considered as friends of `0x42::A`.
40 |
41 | 一个模块可能有多个友元声明,所有好友模块的并集形成友元列表。在下面的示例中`,0x42::B`和`0x42::C`都被视为 的友元函数`0x42::A`。
42 |
43 | ```move
44 | address 0x42 {
45 | module a {
46 | friend 0x42::b;
47 | friend 0x42::c;
48 | }
49 | }
50 | ```
51 |
52 | Unlike `use` statements, `friend` can only be declared in the module scope and not in the expression block scope.
53 | `friend` declarations may be located anywhere a top-level construct (e.g., `use`, `function`, `struct`, etc.) is allowed.
54 | However, for readability, it is advised to place friend declarations near the beginning of the module definition.
55 |
56 | 与`use`语句不同,`friend`只能在模块作用域内声明,而不能在表达式块的作用域内声明。`friend`声明可以位于允许顶层构造的任何位置(例如, `use`, `function,struct`等)是被允许的。但是,为了可读性,建议将友元声明放在模块定义的开头附近。
57 |
58 | Note that the concept of friendship does not apply to Move scripts:
59 | - A Move script cannot declare `friend` modules as doing so is considered meaningless: there is no mechanism to call the function defined in a script.
60 | - A Move module cannot declare `friend` scripts as well because scripts are ephemeral code snippets that are never published to global storage.
61 |
62 | 请注意,友元关系(friendship)的概念不适用于 Move 脚本:
63 | - `Move` 脚本不能声明`friend`模块,因为这样做被认为是无意义的:没有机制可以调用脚本中定义的函数。
64 | - `Move` 模块也不能声明`friend`脚本,因为脚本是永远不会发布到全局存储的临时代码片段。
65 |
66 | ### 友元声明规则(Friend declaration rules)
67 | Friend declarations are subject to the following rules:
68 | 友元声明须遵守以下规则:
69 |
70 | - A module cannot declare itself as a friend
71 | - 一个模块不能将自己声明为友元。
72 |
73 | ```move=
74 | address 0x42 {
75 | module m { friend Self; // 错误! }
76 | // ^^^^ 不能将自己声明为友元
77 | }
78 |
79 | address 0x43 {
80 | module m { friend 0x43::M; // 错误! }
81 | // ^^^^^^^ 不能将自己声明为友元
82 | }
83 | ```
84 |
85 | - Friend modules must be known by the compiler
86 | - 编译器必须知道友元模块
87 |
88 | ```move=
89 | address 0x42 {
90 | module m { friend 0x42::nonexistent; // 错误! }
91 | // ^^^^^^^^^^^^^^^^^ 未绑定的模块 '0x42::nonexistent'
92 | }
93 | ```
94 |
95 | - Friend modules must be within the same account address. (Note: this is not a technical requirement but rather a policy decision which *may* be relaxed later.)
96 |
97 | - 友元模块必须在同一个账号地址内。(注:这不是技术要求,而是以后可能放宽的决策。)
98 |
99 | ```move
100 | address 0x42 {
101 | module m {}
102 | }
103 |
104 | address 0x43 {
105 | module n { friend 0x42::m; // 错误! }
106 | // ^^^^^^^ 不能声明当前地址外的模块作为友元
107 | }
108 | ```
109 |
110 | - 友元关系不能创建循环模块依赖关系(Friends relationships cannot create cyclic module dependencies)
111 |
112 | Cycles are not allowed in the friend relationships, e.g., the relation `0x2::a` friends `0x2::b` friends `0x2::c` friends `0x2::a` is not allowed.
113 | More generally, declaring a friend module adds a dependency upon the current module to the friend module (because the purpose is for the friend to call functions in the current module).
114 | If that friend module is already used, either directly or transitively, a cycle of dependencies would be created.
115 |
116 | 友元关系中不允许循环,例如 `0x2::a` 友元 `0x2::b` 友元 `0x2::c` 友元`0x2::a`是不允许的。更普遍地,声明一个友元模块会将对当前模块的依赖添加到友元模块(因为目的是让友元调用当前模块中的函数)。如果该友元模块已被直接或传递地使用,则将形成一个依赖循环。
117 |
118 | ```move
119 | address 0x2 {
120 | module a {
121 | use 0x2::c;
122 | friend 0x2::b;
123 |
124 | public fun a() {
125 | c::c()
126 | }
127 | }
128 |
129 | module b {
130 | friend 0x2::c; // 错误!
131 | // ^^^^^^ 这个友元关系形成了一个依赖循环: '0x2::a' 使用了 '0x2::c' 但'0x2::b' 同时是 '0x2::a'和'0x2::b' 的友元
132 | }
133 |
134 | module c {
135 | public fun c() {}
136 | }
137 | }
138 | ```
139 |
140 | - The friend list for a module cannot contain duplicates.
141 | - 模块的友元列表不能包含重复项。
142 |
143 | ```move=
144 | address 0x42 {
145 | module a {}
146 |
147 | module m {
148 | use 0x42::a as aliased_a;
149 | friend 0x42::A;
150 | friend aliased_a; // 错误!
151 | // ^^^^^^^^^ 重复的友元声明 '0x42::a'. 模块内的友元声明必须是唯一的
152 | }
153 | }
154 | ```
155 |
--------------------------------------------------------------------------------
/src/global-storage-structure.md:
--------------------------------------------------------------------------------
1 | # 全局存储 —— 结构(Global Storage - Structure)
2 |
3 | The purpose of Move programs is to [read from and write to](./global-storage-operators.md) tree-shaped persistent global storage. Programs cannot access the filesystem, network, or any other data outside of this tree.
4 |
5 | Move 程序的目的是[读取和写入](./global-storage-operators.md)树形的持久全局存储。程序不能访问文件系统、网络或任何此树以外的数据。
6 |
7 | In pseudocode, the global storage looks something like:
8 |
9 | 在伪代码中,全局存储看起来像:
10 |
11 | ```move
12 | struct GlobalStorage {
13 | resources: Map<(address, ResourceType), ResourceValue>
14 | modules: Map<(address, ModuleName), ModuleBytecode>
15 | }
16 | ```
17 |
18 | Structurally, global storage is a [forest](https://en.wikipedia.org/wiki/Tree_(graph_theory)) consisting of trees rooted at an account [`address`](./address.md). Each address can store both [resource](./structs-and-resources.md) data values and [module](./modules-and-scripts.md) code values. As the pseudocode above indicates, each `address` can store at most one resource value of a given type and at most one module with a given name.
19 |
20 | 从结构上讲,全局存储是一个[森林(forest)](https://en.wikipedia.org/wiki/Tree_(graph_theory)),这个森林由以账户[地址(`address`)](./address.md)为根的树组成。每个地址可以存储[资源(resource)](./structs-and-resources.md)数据和[模块(module)](./modules-and-scripts.md)代码。如上面的伪代码所示,每个地址(`address`)最多可以存储一个给定类型的资源值,最多可以存储一个给定名称的模块。
21 |
--------------------------------------------------------------------------------
/src/integers.md:
--------------------------------------------------------------------------------
1 | # 整数 (Integers)
2 |
3 | Move supports three unsigned integer types: `u8`, `u64`, and `u128`. Values of these types range from 0 to a maximum that depends on the size of the type.
4 |
5 | | Type | Value Range |
6 | | -------------------------------- | ------------------------ |
7 | | Unsigned 8-bit integer, `u8` | 0 to 28 - 1 |
8 | | Unsigned 64-bit integer, `u64` | 0 to 264 - 1 |
9 | | Unsigned 128-bit integer, `u128` | 0 to 2128 - 1 |
10 |
11 |
12 | Move 支持三种无符号整数类型:`u8`、`u64` 和 `u128`。这些类型的值范围从 0 到最大值,最大值的具体取值取决于整数类型。
13 |
14 | | 类型 | 取值范围 |
15 | |---------------------------|--------------------------|
16 | | 无符号 8位 整数, `u8` | 0 to 28 - 1 |
17 | | 无符号 64位 整数, `u64` | 0 to 264 - 1 |
18 | | 无符号 128位 整数, `u128` | 0 to 2128 - 1 |
19 |
20 |
21 | ## 字面值(Literal)
22 |
23 | Literal values for these types are specified either as a sequence of digits (e.g.,`112`) or as hex literals, e.g., `0xFF`. The type of the literal can optionally be added as a suffix, e.g., `112u8`. If the type is not specified, the compiler will try to infer the type from the context where the literal is used. If the type cannot be inferred, it is assumed to be `u64`.
24 |
25 | If a literal is too large for its specified (or inferred) size range, an error is reported.
26 |
27 | (在Move中)这些类型的字面值指定为数字序列(例如:112)或十六进制文字(例如:0xFF), 可以选择将字面值的类型定义为后缀, 例如 `112u8`。如果未指定类型,编译器将尝试从使用字面值的上下文推断类型。如果无法推断类型,则默认为 `u64。
28 |
29 | 如果字面值太大,超出其指定的(或推断的)大小范围,则会报错。
30 |
31 | ### 例如:
32 |
33 | ```jsx
34 | // literals with explicit annotations;
35 | let explicit_u8 = 1u8;
36 | let explicit_u64 = 2u64;
37 | let explicit_u128 = 3u128;
38 |
39 | // literals with simple inference
40 | let simple_u8: u8 = 1;
41 | let simple_u64: u64 = 2;
42 | let simple_u128: u128 = 3;
43 |
44 | // literals with more complex inference
45 | let complex_u8 = 1; // inferred: u8
46 | // right hand argument to shift must be u8
47 | let _unused = 10 << complex_u8;
48 |
49 | let x: u8 = 0;
50 | let complex_u8 = 2; // inferred: u8
51 | // arguments to `+` must have the same type
52 | let _unused = x + complex_u8;
53 |
54 | let complex_u128 = 3; // inferred: u128
55 | // inferred from function argument type
56 | function_that_takes_u128(complex_u128);
57 |
58 | // literals can be written in hex
59 | let hex_u8: u8 = 0x1;
60 | let hex_u64: u64 = 0xCAFE;
61 | let hex_u128: u128 = 0xDEADBEEF;
62 | ```
63 |
64 | ## 运算集 (Operations)
65 |
66 | ### 算术运算 (Arithmetic)
67 |
68 | Each of these types supports the same set of checked arithmetic operations. For all of these operations, both arguments (the left and right side operands) must be of the same type. If you need to operate over values of different types, you will need to first perform a cast. Similarly, if you expect the result of the operation to be too large for the integer type, perform a cast to a larger size before performing the operation.
69 |
70 | 每一种(无符号整数)类型都支持相同算术运算集。对于所有这些运算,两个参数(左侧和右侧操作数)必须是同一类型。如果您需要对不同类型的值进行运算,则需要首先执行强制转换。同样,如果您预计运算结果对于当下整数类型来说太大,请在执行运算之前将之转换为更大的整数类型。
71 |
72 | All arithmetic operations abort instead of behaving in a way that mathematical integers would not (e.g., overflow, underflow, divide-by-zero).
73 |
74 | | Syntax | Operation | Aborts If |
75 | | ------ | ------------------- | ---------------------------------------- |
76 | | `+` | addition | Result is too large for the integer type |
77 | | `-` | subtraction | Result is less than zero |
78 | | `*` | multiplication | Result is too large for the integer type |
79 | | `%` | modular division | The divisor is `0` |
80 | | `/` | truncating division | The divisor is `0` |
81 |
82 | ### [Bitwise](https://move-language.github.io/move/integers.html#bitwise)
83 |
84 | 算术运算在遇到异常时将会中止,而不是以上溢、下溢、被零除等数学整数未定义的的方式输出结果。
85 |
86 | | 句法 | 操作 | 中止条件 |
87 | | ------ | ------------| ---------------------------------------- |
88 | | `+` | 加法 | 结果对于整数类型来说太大了 |
89 | | `-` | 减法 | 结果小于零 |
90 | | `*` | 乘法 | 结果对于整数类型来说太大了 |
91 | | `%` | 取余运算 | 除数为 `0` |
92 | | `/` | 截断除法 | 除数为 `0` |
93 |
94 | ### 位运算 (Bitwise)
95 |
96 | The integer types support the following bitwise operations that treat each number as a series of individual bits, either 0 or 1, instead of as numerical integer values.
97 |
98 | Bitwise operations do not abort.
99 |
100 | | Syntax | Operation | Description
101 | |--------|------------|------------
102 | | `&` | bitwise and| Performs a boolean and for each bit pairwise
103 | | `|` | bitwise or | Performs a boolean or for each bit pairwise
104 | | `^` | bitwise xor| Performs a boolean exclusive or for each bit pairwise
105 |
106 |
107 | 整数类型支持下列位运算,即将每个数字视为一系列单独的位:0 或 1,而不是整型数值。
108 |
109 | 位运算不会中止。
110 |
111 | | 句法 | 操作 | 描述 |
112 | | ------ | ----------- | ----------------------------------------------------- |
113 | | `&` | 按位 和 | 对每个位成对执行布尔值和 |
114 | | `|` | 按位或 | 对每个位成对执行布尔值或
115 | | `^` | 按位 异与 | 对每个位成对执行布尔异或 |
116 |
117 | ### 位移 (Bit shift)
118 |
119 | Similar to the bitwise operations, each integer type supports bit shifts. But unlike the other operations, the righthand side operand (how many bits to shift by) must *always* be a `u8` and need not match the left side operand (the number you are shifting).
120 |
121 | Bit shifts can abort if the number of bits to shift by is greater than or equal to `8`, `64`, or `128` for `u8`, `u64`, and `u128` respectively.
122 |
123 | | Syntax | Operation | Aborts if |
124 | | ------ | ----------- | ------------------------------------------------------------ |
125 | | `<<` | shift left | Number of bits to shift by is greater than the size of the integer type |
126 | | `>>` | shift right | Number of bits to shift by is greater than the size of the integer type |
127 |
128 | 与按位运算类似,每种整数类型都支持位移(bit shifts)。但与其他运算不同的是,右侧操作数(要移位多少位)必须始终是 `u8` 并且不需要与左侧操作数类型(您要移位的数字)匹配。
129 |
130 | 如果要移位的位数分别大于或等于 `8`、`64`, `u128` 或 `128` 的 `u8`, `u64`, 则移位可以中止。
131 |
132 | | 句法 | 操作 | 中止条件 |
133 | | ------ | ----------- | ------------------------------------------------------------ |
134 | | `<<` | 左移 | 要移位的位数大于整数类型的大小 |
135 | | `>>` | 右移 | 要移位的位数大于整数类型的大小 |
136 |
137 | ### 比较运算 (Comparisons)
138 |
139 | Integer types are the *only* types in Move that can use the comparison operators. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to [cast](https://move-language.github.io/move/integers.html#casting) one of them first.
140 |
141 | Comparison operations do not abort.
142 |
143 | | Syntax | Operation |
144 | | ------ | ------------------------ |
145 | | `<` | less than |
146 | | `>` | greater than |
147 | | `<=` | less than or equal to |
148 | | `>=` | greater than or equal to |
149 |
150 | 整数类型是 Move 中唯一可以使用比较(Comparisons)运算符的类型。两个参数必须是同一类型。如果您需要比较不同类型的整数,则需要先转换其中一个。
151 |
152 | 比较操作不会中止。
153 |
154 | | 句法 | 操作 |
155 | | ------ | ------------------------ |
156 | | `<` | 小于 |
157 | | `>` | 大于 |
158 | | `<=` | 小于等于 |
159 | | `>=` | 大于等于 |
160 |
161 | ### 相等 (Equality)
162 |
163 | Like all types with [`drop`](https://move-language.github.io/move/abilities.html) in Move, all integer types support the ["equal"](https://move-language.github.io/move/equality.html) and ["not equal"](https://move-language.github.io/move/equality.html) operations. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to [cast](https://move-language.github.io/move/integers.html#casting) one of them first.
164 |
165 | Equality operations do not abort.
166 |
167 | | Syntax | Operation |
168 | | ------ | --------- |
169 | | `==` | equal |
170 | | `!=` | not equal |
171 |
172 | For more details see the section on [equality](https://move-language.github.io/move/equality.html)
173 |
174 | 与 Move 中的所有具有[`drop`](./chapter_19_abilities.html)能力的类型一样,所有整数类型都支持 ["equal(等于)"](./chapter_11_equality.html) 和 ["not equal(不等于)](./chapter_11_equality.html)运算。两个参数必须是同一类型。如果您需要比较不同类型的整数,则需要先转换其中一个。
175 |
176 | 相等(Equality)运算不会中止。
177 |
178 | | 句法 | 操作 |
179 | | ------ | --------- |
180 | | `==` | 等于 |
181 | | `!=` | 不等于 |
182 |
183 | 更多细节可以参考[相等]([equality](https://move-language.github.io/move/equality.html))章节。
184 |
185 | ## 转换 (Casting)
186 |
187 | Integer types of one size can be cast to integer types of another size. Integers are the only types in Move that support casting.
188 |
189 | Casts *do not* truncate. Casting will abort if the result is too large for the specified type
190 |
191 | | Syntax | Operation | Aborts if |
192 | | ---------- | ---------------------------------------------------- | -------------------------------------- |
193 | | `(e as T)` | Cast integer expression `e` into an integer type `T` | `e` is too large to represent as a `T` |
194 |
195 | Here, the type of `e` must be `u8`, `u64`, or `u128` and `T` must be `u8`, `u64`, or `u128`.
196 |
197 | For example:
198 |
199 | - `(x as u8)`
200 | - `(2u8 as u64)`
201 | - `(1 + 3 as u128)`
202 |
203 | 一种大小的整数类型可以转换为另一种大小的整数类型。整数是 Move 中唯一支持强制转换的类型。
204 |
205 | 强制转换不会截断。如果结果对于指定类型来说太大,则转换将中止。
206 |
207 | | Syntax | 操作 | 中止条件 |
208 | | ---------- | ---------------------------------------------------- | -------------------------------------- |
209 | | `(e as T)` | 将整数表达式 `e` 转换为整数类型 `T` | `e` 太大而不能表示为 `T` |
210 |
211 | ## 所有权 (Ownership)
212 |
213 | As with the other scalar values built-in to the language, integer values are implicitly copyable, meaning they can be copied without an explicit instruction such as [`copy`](https://move-language.github.io/move/variables.html#move-and-copy).
214 |
215 | 与语言内置的其他标量值一样,整数值是隐式可复制的,这意味着它们可以在没有明确指令如[`copy`](./variables.md#move-and-copy)的情况下复制。
216 |
--------------------------------------------------------------------------------
/src/introduction.md:
--------------------------------------------------------------------------------
1 | # 引言 (Introduction)
2 |
3 | Welcome to Move, a next generation language for secure, sandboxed, and formally verified programming. Its first use case is for the Diem blockchain, where Move provides the foundation for its implementation. Move allows developers to write programs that flexibly manage and transfer assets, while providing the security and protections against attacks on those assets. However, Move has been developed with use cases in mind outside a blockchain context as well.
4 |
5 | 欢迎来到Move的世界,Move是一种安全、沙盒式和形式化验证的下一代编程语言,它的第一个用例是 Diem 区块链(当时名字叫Libra, 脸书团队开发的项目, 译者注), Move 为其实现提供了基础。 Move 允许开发人员编写灵活管理和转移数字资产的程序,同时提供安全保护,防止对那些链上资产的攻击。不仅如此,Move 也可用于区块链世界之外的开发场景。
6 |
7 | Move takes its cue from [Rust](https://www.rust-lang.org/) by using resource types with move (hence the name) semantics as an explicit representation of digital assets, such as currency.
8 |
9 | Move 的诞生从[Rust](https://www.rust-lang.org/)中吸取了灵感,Move也是因为使用具有移动(move)语义的资源类型作为数字资产(例如货币)的显式表示而得名。
10 |
11 | ## Move是为谁准备的?(Who is Move for?)
12 |
13 | Move was designed and created as a secure, verified, yet flexible programming language. The first use of Move is for the implementation of the Diem blockchain. That said, the language is still evolving. Move has the potential to be a language for other blockchains, and even non-blockchain use cases as well.
14 |
15 | Move语言被设计和创建为安全、可验证, 同时兼顾灵活性的编程语言。Move的第一个应用场景是用于Diem区块链的开发。现在,Move语言仍在不断发展中。Move 还有成为其他区块链,甚至非区块链用例开发语言的潜质。
16 |
17 | Given custom Move modules will not be supported at the [launch](https://diem.com/white-paper/#whats-next) of the Diem Payment Network (DPN), we are targeting an early Move Developer persona.
18 |
19 | 鉴于在 Diem 支付网络 (DPN) [启动](https://diem.com/white-paper/#whats-next)时将不支持自定义 Move 模块(custom Move modules),我们的目标是早期的 Move 开发人员。
20 |
21 | The early Move Developer is one with some programming experience, who wants to begin understanding the core programming language and see examples of its usage.
22 |
23 | 早期的 Move 开发人员应该是具有一定编程经验的程序员,他们愿意了解编程语言核心,并探索它的用法。
24 |
25 | ### 爱好者 (Hobbyists)
26 |
27 | Understanding that the capability to create custom modules on the Diem Payment Network will not be available at launch, the hobbyist Move Developer is interested in learning the intricacies of the language. She will understand the basic syntax, the standard libraries available, and write example code that can be executed using the Move CLI. The Move Developer may even want to dig into understanding how the Move Virtual Machine executes the code she writes.
28 |
29 | 作为(Move语言)爱好者角色,首先需要明白在Diem支付网络上创建自定义模块(custom modules)是不可能的,其次,你还要对探索这门语言的复杂性保持兴趣。你将了解基本语法、可用的标准库,并编写可以用的Move CLI执行的示例代码。如果可能,你甚至可以去尝试体验Move虚拟机如何执行你自己编写的代码。
30 |
31 | ### 核心贡献者 (Core Contributor)
32 |
33 | Beyond a hobbyist wanting to stay ahead of the curve for the core programming language is someone who may want to [contribute](https://diem.com/en-US/cla-sign/) directly to Move. Whether this includes submitting language improvements or even, in the future, adding core modules available on the Diem Payment Network, the core contributor will understand Move at a deep level.
34 |
35 | 核心贡献者指那些超越爱好者并想在核心编程语言方面保持领先,还直接为 Move 做出[贡献](https://diem.com/en-US/cla-sign/)的人。无论是提交语言改进,甚至未来添加 Diem 支付网络上可用的核心模块等,核心贡献者都将深入了解Move。
36 |
37 | ### Move不适用于哪些人?(Who Move is currently not targeting)
38 |
39 | Currently, Move is not targeting developers who wish to create custom modules and contracts for use on the Diem Payment Network. We are also not targeting novice developers who expect a completely polished developer experience even in testing the language.
40 |
41 | 目前,Move 并不适用那些希望在在 Diem 支付网络上创建自定义模块和合约的开发人员。我们也不针对期望在测试语言时就能获得完美开发体验的初学开发者。
42 |
43 | ## 从哪里开始?(Where Do I Start?)
44 |
45 | Begin with understanding [modules and scripts](https://move-language.github.io/move/modules-and-scripts.html) and then work through the [Move Tutorial](https://move-language.github.io/move/creating-coins.html).
46 |
47 | 你可以从了解模块和脚本([modules and scripts](./modules-and-scripts.html))开始,然后跟随Move教程([Move Tutorial](./move-tutorial.html))进行练习。
48 |
--------------------------------------------------------------------------------
/src/loops.md:
--------------------------------------------------------------------------------
1 | # While and Loop
2 |
3 | Move offers two constructs for looping: `while` and `loop`.
4 |
5 | Move 提供了两种循环结构: `while` and `loop`.
6 |
7 | ## `while` 循环
8 |
9 | The `while` construct repeats the body (an expression of type unit) until the condition (an expression of type `bool`) evaluates to `false`.
10 |
11 | Here is an example of simple `while` loop that computes the sum of the numbers from `1` to `n`:
12 |
13 | `while` 会重复执行结构(一个 `unit` 类型的表达式), 直到条件语句(`bool` 类型的表达式)运算结果为 `false`。
14 |
15 | 下面是一个简单的 `while` 循环的例子,计算从 `1` 到 `n` 数字之和:
16 |
17 | ```move
18 | fun sum(n: u64): u64 {
19 | let sum = 0;
20 | let i = 1;
21 | while (i <= n) {
22 | sum = sum + i;
23 | i = i + 1
24 | };
25 |
26 | sum
27 | }
28 | ```
29 |
30 | Infinite loops are allowed:
31 |
32 | 无限循环是被允许的:
33 |
34 | ```move=
35 | fun foo() {
36 | while (true) { }
37 | }
38 | ```
39 |
40 | ### `break`
41 |
42 | The `break` expression can be used to exit a loop before the condition evaluates to `false`. For example, this loop uses `break` to find the smallest factor of `n` that's greater than 1:
43 |
44 | `break` 表达式可用于在条件计算结果为 `false` 之前退出循环。例如,这个循环使用 `break` 查找 `n` 大于1的最小因子:
45 |
46 | ```move
47 | fun smallest_factor(n: u64): u64 {
48 | // assuming the input is not 0 or 1
49 | let i = 2;
50 | while (i <= n) {
51 | if (n % i == 0) break;
52 | i = i + 1
53 | };
54 |
55 | i
56 | }
57 | ```
58 |
59 | The `break` expression cannot be used outside of a loop.
60 |
61 | `break` 表达式不能在循环之外使用。
62 |
63 | ### `continue`
64 |
65 | The `continue` expression skips the rest of the loop and continues to the next iteration. This loop uses `continue` to compute the sum of `1, 2, ..., n`, except when the number is divisible by 10:
66 |
67 | `continue` 表达式跳过当前循环的剩余部分, 并继续下一轮迭代。下面的例子, 使用 `continue` 去计算 `1, 2, ..., n` 的总和,过滤掉不能被10整除的数:
68 |
69 | ```move
70 | fun sum_intermediate(n: u64): u64 {
71 | let sum = 0;
72 | let i = 0;
73 | while (i < n) {
74 | i = i + 1;
75 | if (i % 10 == 0) continue;
76 | sum = sum + i;
77 | };
78 |
79 | sum
80 | }
81 | ```
82 |
83 | The `continue` expression cannot be used outside of a loop.
84 |
85 | `continue` 表达式不能在循环之外使用。
86 |
87 | ### The type of `break` and `continue`
88 |
89 | `break` and `continue`, much like `return` and `abort`, can have any type. The following examples illustrate where this flexible typing can be helpful:
90 |
91 | `break` and `continue`, 和 `return` and `abort` 很相像, 可以是任何类型。下面的例子说明了这种灵活的类型在那些方面有帮助:
92 |
93 | ```move
94 | fun pop_smallest_while_not_equal(
95 | v1: vector,
96 | v2: vector,
97 | ): vector {
98 | let result = vector::empty();
99 | while (!vector::is_empty(&v1) && !vector::is_empty(&v2)) {
100 | let u1 = *vector::borrow(&v1, vector::length(&v1) - 1);
101 | let u2 = *vector::borrow(&v2, vector::length(&v2) - 1);
102 | let popped =
103 | if (u1 < u2) vector::pop_back(&mut v1)
104 | else if (u2 < u1) vector::pop_back(&mut v2)
105 | else break; // Here, `break` has type `u64`
106 | vector::push_back(&mut result, popped);
107 | };
108 |
109 | result
110 | }
111 |
112 | fun pick(
113 | indexes: vector,
114 | v1: &vector,
115 | v2: &vector
116 | ): vector {
117 | let len1 = vector::length(v1);
118 | let len2 = vector::length(v2);
119 | let result = vector::empty();
120 | while (!vector::is_empty(&indexes)) {
121 | let index = vector::pop_back(&mut indexes);
122 | let chosen_vector =
123 | if (index < len1) v1
124 | else if (index < len2) v2
125 | else continue; // Here, `continue` has type `&vector`
126 | vector::push_back(&mut result, *vector::borrow(chosen_vector, index))
127 | };
128 |
129 | result
130 | }
131 | ```
132 |
133 | ## `loop`表达式
134 |
135 | The `loop` expression repeats the loop body (an expression with type `()`) until it hits a `break`
136 |
137 | Without a `break`, the loop will continue forever
138 |
139 | `loop` 表达式重复循环体(类型为unit()的表达式) ,直到遇到 `break` 为止。
140 |
141 | (下面的代码中)没有 `break`, 循环将一直执行。
142 |
143 | ```move
144 | fun foo() {
145 | let i = 0;
146 | loop { i = i + 1 }
147 | }
148 | ```
149 |
150 | Here is an example that uses `loop` to write the `sum` function:
151 |
152 | 这是一个使用 `loop` 编写 `sum` 函数的示例(可与 `while` 循环比较):
153 |
154 | ```move
155 | fun sum(n: u64): u64 {
156 | let sum = 0;
157 | let i = 0;
158 | loop {
159 | i = i + 1;
160 | if (i > n) break;
161 | sum = sum + i
162 | };
163 |
164 | sum
165 | }
166 | ```
167 |
168 | As you might expect, `continue` can also be used inside a `loop`. Here is `sum_intermediate` from above rewritten using `loop` instead of `while`
169 |
170 | 正如你所料, `continue` 也可以在 `loop` 中使用。这是上面的 `sum_intermediate` 使用 `loop` 代替 `while` 重写的:
171 |
172 | ```move
173 | fun sum_intermediate(n: u64): u64 {
174 | let sum = 0;
175 | let i = 0;
176 | loop {
177 | i = i + 1;
178 | if (i % 10 == 0) continue;
179 | if (i > n) break;
180 | sum = sum + i
181 | };
182 |
183 | sum
184 | }
185 | ```
186 |
187 | ## `while` and `loop` 的类型
188 |
189 | Move loops are typed expressions. A `while` expression always has type `()`.
190 |
191 | Move 循环是有类型化的表达式。 `while` 表达式始终具有 `()` 类型。
192 |
193 | ```move
194 | let () = while (i < 10) { i = i + 1 };
195 | ```
196 |
197 | If a `loop` contains a `break`, the expression has type unit `()`
198 |
199 | 如果 `loop` 中包含 `break` , 这个表达式的类型则为 unit `()`
200 |
201 |
202 | ```move
203 | (loop { if (i < 10) i = i + 1 else break }: ());
204 | let () = loop { if (i < 10) i = i + 1 else break };
205 | ```
206 |
207 | If `loop` does not have a `break`, `loop` can have any type much like `return`, `abort`, `break`, and `continue`.
208 |
209 | 如果 `loop` 不包含 `break`, `loop` 可以是任何类型, 就像`return`, `abort`, `break`, 和 `continue`。
210 |
211 | ```move
212 | (loop (): u64);
213 | (loop (): address);
214 | (loop (): &vector>);
215 | ```
216 |
--------------------------------------------------------------------------------
/src/modules-and-scripts.md:
--------------------------------------------------------------------------------
1 | # 模块和脚本 (Modules and Scripts)
2 |
3 | Move has two different types of programs: ***Modules*** and ***Scripts***. Modules are libraries that define struct types along with functions that operate on these types. Struct types define the schema of Move's [global storage](./global-storage-structure.md), and module functions define the rules for updating storage. Modules themselves are also stored in global storage. Scripts are executable entrypoints similar to a `main` function in a conventional language. A script typically calls functions of a published module that perform updates to global storage. Scripts are ephemeral code snippets that are not published in global storage.
4 |
5 | A Move source file (or **compilation unit**) may contain multiple modules and scripts. However, publishing a module or executing a script are separate VM operations.
6 |
7 | Move有两种不同类型的程序: ***Modules***和 ***Scripts***。模块(Modules, 相当于智能合约,译者注)是定义结构类型以及对这些类型进行操作的函数的库。结构类型定义Move的[全局存储](./global-storage-structure.md)的模式,模块函数定义更新存储的规则。模块本身也存储在全局存储中。脚本(Scripts)是可执行的入口点,类似于传统语言中的主函数 `main`。脚本通常调用已发布模块的函数来更新全局存储。Scripts是暂时的代码片段,没有发布到全局存储中。
8 |
9 | 一个Move源文件(或**编译单元**)可能包含多个模块和脚本。然而,发布模块或执行脚本都是独立的VM操作。
10 |
11 | ## 语法(Syntax)
12 |
13 | ### 脚本(Scripts)
14 |
15 | A script has the following structure:
16 | script具有以下结构:
17 |
18 | ```text
19 | script {
20 |