Skip to content

Commit

Permalink
fix: 应用 --> 引用 (#28)
Browse files Browse the repository at this point in the history
* fix: 应用 --> 引用

* fix: starts_width -> starts_with
  • Loading branch information
WShiBin authored Jun 26, 2023
1 parent 24d4421 commit a892df3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions md/ch03.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 基本类型

*这个世界上有很多很多不同类型的书,这是一件好事。但也有很多很多不同类型的人,每个人都想读到一些不同的东西。*
*这个世界上有很多很多不同类型的书,这是一件好事。但也有很多很多不同类型的人,每个人都想读到一些不同的东西。*
<p align="right">
——Lemony Snicket
</p>
Expand Down Expand Up @@ -496,7 +496,7 @@ Rust的指针有以下两种类型:

*`&mut T`*

&emsp;&emsp;一个可变的、独占的应用。你可以读写它指向的值,类似于C中的`T*`。但只要这个引用存在,你不能再持有任何这个值的其他任何类型的引用。事实上,这时候你唯一可以访问这个值的方法就是通过这个可变引用。
&emsp;&emsp;一个可变的、独占的引用。你可以读写它指向的值,类似于C中的`T*`。但只要这个引用存在,你不能再持有任何这个值的其他任何类型的引用。事实上,这时候你唯一可以访问这个值的方法就是通过这个可变引用。

Rust使用这种方式来区分共享和可变的引用,以此来强制执行“单个写者或多个读者”规则:要么你可以读写值,要么它只能被任何数量的读者共享。这种分隔由编译器检查来强制执行,它是Rust安全保证的核心。”第5章”解释了Rust中使用安全引用的规则。

Expand Down
14 changes: 7 additions & 7 deletions md/ch05.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 引用

*Libraries cannot provide new inabilities.*
*Libraries cannot provide new inabilities.*

<p align="right">
——Mark Miller
Expand Down Expand Up @@ -537,7 +537,7 @@ Rust怎么处理存储在数据结构中的引用?这里有一个我们之前
struct S {
r: &i32
}

let s;
{
let x = 10;
Expand Down Expand Up @@ -737,7 +737,7 @@ Rust为什么会报错?如果你仔细思考这段代码,你会发现以下
impl StringTable {
fn find_by_prefi(&self, prefix: &str) -> Option<&String> {
for i in 0 .. self.elements.len() {
if self.elements[i].starts_width(prefix) {
if self.elements[i].starts_with(prefix) {
return Some(&self.elements[i]);
}
}
Expand Down Expand Up @@ -963,14 +963,14 @@ Rust会报告`extend`的例子违反了第二条规则:因为我们已经借

当然,Rust拒绝编译代码:
```
error[E0502] : cannot borrow `f` as immutable because it is also
borrowed as mutable
error[E0502] : cannot borrow `f` as immutable because it is also
borrowed as mutable
--> references_self_assignment.rs:18:25
|
18 | clone_from(&mut f, &f);
| - ^- mutable borrow ends here
| - ^- mutable borrow ends here
| | |
| | immutable borrow occurs here
| | immutable borrow occurs here
| mutable borrow occurs here
```

Expand Down
12 changes: 6 additions & 6 deletions md/ch16.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 集合

*We all behave like Maxwell’s demon. Organisms organize. In everyday experience lies the reason sober physicists across two centuries kept this cartoon fantasy alive. We sort the mail, build sand castles, solve jigsaw puzzles, separate wheat from chaff, rearrange chess pieces, collect stamps, alphabetize books, create symmetry, compose sonnets and sonatas, and put our rooms in order, and all this we do requires no great energy, as long as we can apply intelligence.*
*We all behave like Maxwell’s demon. Organisms organize. In everyday experience lies the reason sober physicists across two centuries kept this cartoon fantasy alive. We sort the mail, build sand castles, solve jigsaw puzzles, separate wheat from chaff, rearrange chess pieces, collect stamps, alphabetize books, create symmetry, compose sonnets and sonatas, and put our rooms in order, and all this we do requires no great energy, as long as we can apply intelligence.*

<p align="right">
——James Gleick, The Information: A History, a Theory, a Flood
Expand Down Expand Up @@ -95,7 +95,7 @@ Rust标准库里包含几种 *集合(collection)* ,它们是在内存中存储
// 获取一个元素的引用
let first_line = &lines[0];

// 获取一个元素的拷贝
// 获取一个元素的拷贝
let fifth_number = numbers[4]; // 需要Copy
let second_number = lines[1].clone(); // 需要Clone

Expand Down Expand Up @@ -581,8 +581,8 @@ vector有一个高效地移除任何元素的方法:
&emsp;&emsp;如果`slice`起始的值序列等于`other`的元素则返回`true`

```Rust
assert_eq!([1, 2, 3, 4].starts_width(&[1, 2]), true);
assert_eq!([1, 2, 3, 4].starts_width(&[2, 3]), false);
assert_eq!([1, 2, 3, 4].starts_with(&[1, 2]), true);
assert_eq!([1, 2, 3, 4].starts_with(&[2, 3]), false);
```

*`slice.ends_with(other)`*
Expand Down Expand Up @@ -625,7 +625,7 @@ Rust的标准库中并不包含随机数。`rand` crate提供了它们,还提
for index, val in enumerate(my_list):
if val > 4:
del my_list[index] # bug: 在迭代的同时修改

print(my_list)
```
(Python中的`enumerate`函数等价于Rust中的`.enumerate()`方法,见“`enumerate`”。)
Expand All @@ -637,7 +637,7 @@ Rust的标准库中并不包含随机数。`rand` crate提供了它们,还提
```Rust
fn main() {
let mut my_vec = vec![1, 3, 5, 7, 9];

for (index, &val) in my_vec.iter().enumerate() {
if val > 4 {
my_vec.remove(index); // error: can't borrow `my_vec` as mutable
Expand Down
14 changes: 7 additions & 7 deletions src/ch03.tex
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ \section{固定位数的数字类型}
\textbf{大小(比特数)} & \textbf{无符号整数} & \textbf{有符号整数} & \textbf{浮点数} \\
\hline
\texttt{8} & \texttt{u8} & \texttt{i8} & \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
\texttt{16} & \texttt{u16} & \texttt{i16} & \\
\texttt{32} & \texttt{u32} & \texttt{i32} & \texttt{f32} \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
\texttt{64} & \texttt{u64} & \texttt{i64} & \texttt{f64} \\
\texttt{128}& \texttt{u128} & \texttt{i128} & \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
机器字 & \texttt{usize} & \texttt{isize} & \\
\end{tabular}
\end{table}
Expand All @@ -132,13 +132,13 @@ \subsection{整数类型}
\textbf{类型} & \textbf{范围} \\
\hline
\texttt{u8} & 0到$2^{8}-1$(0到255) \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
\texttt{u16} & 0到$2^{16}-1$(0到65,535) \\
\texttt{u32} & 0到$2^{32}-1$(0到4,294,967,295) \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
\texttt{u64} & 0到$2^{64}-1$(0到18,446,744,073,709,551,615或1万8千亿) \\
\texttt{u128} & 0到$2^{128}-1$(0到大约$3.4*10^{38}$) \\
\rowcolor{tablecolor}
\rowcolor{tablecolor}
\texttt{usize} & 0到$2^{32}-1$$2^{64}-1$ \\
\end{tabular}
\end{table}
Expand Down Expand Up @@ -595,7 +595,7 @@ \subsection{引用}
\hangparagraph{一个不可变的共享引用。你可以同时拥有同一个值的多个共享的引用,但它们都是只读的:修改它们指向的值是禁止的,就像C中的\texttt{const T*}一样。}

\codeentry{\&mut T}
\hangparagraph{一个可变的、独占的应用。你可以读写它指向的值,类似于C中的\texttt{T*}。但只要这个引用存在,你不能再持有任何这个值的其他任何类型的引用。事实上,这时候你唯一可以访问这个值的方法就是通过这个可变引用。}
\hangparagraph{一个可变的、独占的引用。你可以读写它指向的值,类似于C中的\texttt{T*}。但只要这个引用存在,你不能再持有任何这个值的其他任何类型的引用。事实上,这时候你唯一可以访问这个值的方法就是通过这个可变引用。}

Rust使用这种方式来区分共享和可变的引用,以此来强制执行“单个写者或多个读者”规则:要么你可以读写值,要么它只能被任何数量的读者共享。这种分隔由编译器检查来强制执行,它是Rust安全保证的核心。\hyperref[ch05]{第5章}解释了Rust中使用安全引用的规则。

Expand Down
14 changes: 7 additions & 7 deletions src/ch05.tex
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ \subsection{包含引用的结构体}\label{refstruct}
struct S {
r: &i32
}
let s;
{
let x = 10;
Expand Down Expand Up @@ -756,7 +756,7 @@ \subsection{省略生命周期参数}\label{OmitLifeTime}
impl StringTable {
fn find_by_prefi(&self, prefix: &str) -> Option<&String> {
for i in 0 .. self.elements.len() {
if self.elements[i].starts_width(prefix) {
if self.elements[i].starts_with(prefix) {
return Some(&self.elements[i]);
}
}
Expand Down Expand Up @@ -998,14 +998,14 @@ \section{共享vs可变}\label{ShareVSMut}
当然,Rust拒绝编译代码:
\begin{minted}{text}
error[E0502] : cannot borrow `f` as immutable because it is also
borrowed as mutable
error[E0502] : cannot borrow `f` as immutable because it is also
borrowed as mutable
--> references_self_assignment.rs:18:25
|
18 | clone_from(&mut f, &f);
| - ^- mutable borrow ends here
| - ^- mutable borrow ends here
| | |
| | immutable borrow occurs here
| | immutable borrow occurs here
| mutable borrow occurs here
\end{minted}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ \section{共享vs可变}\label{ShareVSMut}
\end{minted}
为了确保值是常量,我们需要持续追踪所有可以访问到这个值的路径,并确保它们都不允许修改操作或者直接不可用。C和C++的编译器对指针的检查太过宽松。Rust的引用总是关联到一个特定的生命周期,这使得在编译期检查它们变得可行。
\end{shaded}
\end{shaded}
\clearpage
Expand Down
12 changes: 6 additions & 6 deletions src/ch16.tex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ \section{概述}
\cline{3-5}
& & \textbf{C++} & \textbf{Java} & \textbf{Python} \\
\hline

\texttt{Vec<T>} & 可增长的数组 & \texttt{vector} & \texttt{ArrayList} & \texttt{list} \\
\rowcolor{tablecolor}
\texttt{VecDeque<T>} & 双端队列(可增长环形缓冲区) & \texttt{deque} & \texttt{ArrayDeque} & \texttt{collections.deque} \\
Expand Down Expand Up @@ -107,7 +107,7 @@ \subsection{访问元素}
// 获取一个元素的引用
let first_line = &lines[0];

// 获取一个元素的拷贝
// 获取一个元素的拷贝
let fifth_number = numbers[4]; // 需要Copy
let second_number = lines[1].clone(); // 需要Clone

Expand Down Expand Up @@ -507,8 +507,8 @@ \subsection{比较切片}
\codeentry{slice.starts\_with(other)}
\hangparagraph{如果\texttt{slice}起始的值序列等于\texttt{other}的元素则返回\texttt{true}:}
\begin{minted}{Rust}
assert_eq!([1, 2, 3, 4].starts_width(&[1, 2]), true);
assert_eq!([1, 2, 3, 4].starts_width(&[2, 3]), false);
assert_eq!([1, 2, 3, 4].starts_with(&[1, 2]), true);
assert_eq!([1, 2, 3, 4].starts_with(&[2, 3]), false);
\end{minted}

\codeentry{slice.ends\_with(other)}
Expand Down Expand Up @@ -545,7 +545,7 @@ \subsection{Rust排除了无效性错误}
for index, val in enumerate(my_list):
if val > 4:
del my_list[index] # bug: 在迭代的同时修改

print(my_list)
\end{minted}
(Python中的\texttt{enumerate}函数等价于Rust中的\texttt{.enumerate()}方法,见\nameref{enumerate}。)
Expand All @@ -557,7 +557,7 @@ \subsection{Rust排除了无效性错误}
\begin{minted}{Rust}
fn main() {
let mut my_vec = vec![1, 3, 5, 7, 9];

for (index, &val) in my_vec.iter().enumerate() {
if val > 4 {
my_vec.remove(index); // error: can't borrow `my_vec` as mutable
Expand Down

0 comments on commit a892df3

Please sign in to comment.