Skip to content

Commit 6a4b5b3

Browse files
committed
basic concept
1 parent 473678b commit 6a4b5b3

File tree

178 files changed

+4075
-0
lines changed

Some content is hidden

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

178 files changed

+4075
-0
lines changed

.DS_Store

8 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{2.0}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// f64}
4+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{y}: \PYG{k+kt}{f32} \PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{3.0}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// f32}
5+
\PYG{p}{\PYGZcb{}}
6+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s+sc}{b\PYGZsq{}A\PYGZsq{}}
3+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mo}{0o77}
3+
\end{Verbatim}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo new hello\PYGZus{}cargo
3+
\PYGZdl{} \PYG{n+nb}{cd} hello\PYGZus{}cargo
4+
\end{Verbatim}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{s1}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{String}::\PYG{n}{from}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}hello\PYGZdq{}}\PYG{p}{);}
3+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{s2}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{s1}\PYG{p}{;}
4+
5+
\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}\PYGZob{}\PYGZcb{}, world!\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{s1}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// Error!}
6+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}
3+
\end{Verbatim}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{spaces}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{} \PYGZdq{}}\PYG{p}{;}
4+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{spaces}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{spaces}\PYG{p}{.}\PYG{n}{len}\PYG{p}{();}
5+
\PYG{p}{\PYGZcb{}}
6+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} curl \PYGZhy{}\PYGZhy{}proto \PYG{l+s+s1}{\PYGZsq{}=https\PYGZsq{}} \PYGZhy{}\PYGZhy{}tlsv1.3 https://sh.rustup.rs \PYGZhy{}sSf \PYG{p}{|} sh
3+
\end{Verbatim}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{s}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{String}::\PYG{n}{from}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}hello\PYGZdq{}}\PYG{p}{);}
4+
5+
\PYG{+w}{ }\PYG{n}{change}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{s}\PYG{p}{);}
6+
\PYG{p}{\PYGZcb{}}
7+
8+
\PYG{k}{fn} \PYG{n+nf}{change}\PYG{p}{(}\PYG{n}{some\PYGZus{}string}: \PYG{k+kp}{\PYGZam{}}\PYG{n+nb}{String}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
9+
\PYG{+w}{ }\PYG{n}{some\PYGZus{}string}\PYG{p}{.}\PYG{n}{push\PYGZus{}str}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}, world\PYGZdq{}}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// Attempting to modify a borrowed value!}
10+
\PYG{p}{\PYGZcb{}}
11+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{const}\PYG{+w}{ }\PYG{n}{THREE\PYGZus{}HOURS\PYGZus{}IN\PYGZus{}SECONDS}: \PYG{k+kt}{u32} \PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{60}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{l+m+mi}{60}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{;}
3+
\end{Verbatim}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo run
3+
Compiling ownership v0.1.0 \PYG{o}{(}file:///projects/ownership\PYG{o}{)}
4+
error\PYG{o}{[}E0596\PYG{o}{]}: cannot borrow \PYG{l+s+sb}{`}*some\PYGZus{}string\PYG{l+s+sb}{`} as mutable, as it is behind a \PYG{l+s+sb}{`}\PYG{p}{\PYGZam{}}\PYG{l+s+sb}{`} reference
5+
\PYGZhy{}\PYGZhy{}\PYGZgt{} src/main.rs:8:5
6+
\PYG{p}{|}
7+
\PYG{l+m}{7} \PYG{p}{|} fn change\PYG{o}{(}some\PYGZus{}string: \PYG{p}{\PYGZam{}}String\PYG{o}{)} \PYG{o}{\PYGZob{}}
8+
\PYG{p}{|} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} help: consider changing this to be a mutable reference: \PYG{l+s+sb}{`}\PYG{p}{\PYGZam{}}mut String\PYG{l+s+sb}{`}
9+
\PYG{l+m}{8} \PYG{p}{|} some\PYGZus{}string.push\PYGZus{}str\PYG{o}{(}\PYG{l+s+s2}{\PYGZdq{}, world\PYGZdq{}}\PYG{o}{)}\PYG{p}{;}
10+
\PYG{p}{|} \PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{} \PYG{l+s+sb}{`}some\PYGZus{}string\PYG{l+s+sb}{`} is a \PYG{l+s+sb}{`}\PYG{p}{\PYGZam{}}\PYG{l+s+sb}{`} reference, so the data it refers to cannot be borrowed as mutable
11+
12+
For more information about this error, try \PYG{l+s+sb}{`}rustc \PYGZhy{}\PYGZhy{}explain E0596\PYG{l+s+sb}{`}.
13+
error: could not compile \PYG{l+s+sb}{`}ownership\PYG{l+s+sb}{`} due to previous error
14+
\end{Verbatim}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{five}\PYG{p}{()}\PYG{+w}{ }\PYGZhy{}\PYGZgt{} \PYG{k+kt}{i32} \PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{+w}{ }\PYG{c+c1}{// no semicolon because it’s an expression whose value we want to return}
4+
\PYG{p}{\PYGZcb{}}
5+
6+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
7+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{five}\PYG{p}{();}
8+
9+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The value of x is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// 6}
10+
\PYG{p}{\PYGZcb{}}
11+
\end{Verbatim}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo run
3+
Compiling variables v0.1.0 \PYG{o}{(}file:///projects/variables\PYG{o}{)}
4+
error\PYG{o}{[}E0384\PYG{o}{]}: cannot assign twice to immutable variable \PYG{l+s+sb}{`}x\PYG{l+s+sb}{`}
5+
\PYGZhy{}\PYGZhy{}\PYGZgt{} src/main.rs:4:5
6+
\PYG{p}{|}
7+
\PYG{l+m}{2} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{x} \PYG{o}{=} \PYG{l+m}{5}\PYG{p}{;}
8+
\PYG{p}{|} \PYGZhy{}
9+
\PYG{p}{|} \PYG{p}{|}
10+
\PYG{p}{|} first assignment to \PYG{l+s+sb}{`}x\PYG{l+s+sb}{`}
11+
\PYG{p}{|} help: consider making this binding mutable: \PYG{l+s+sb}{`}mut x\PYG{l+s+sb}{`}
12+
\PYG{l+m}{3} \PYG{p}{|} println!\PYG{o}{(}\PYG{l+s+s2}{\PYGZdq{}The value of x is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{o}{)}\PYG{p}{;}
13+
\PYG{l+m}{4} \PYG{p}{|} \PYG{n+nv}{x} \PYG{o}{=} \PYG{l+m}{6}\PYG{p}{;}
14+
\PYG{p}{|} \PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{} cannot assign twice to immutable variable
15+
16+
For more information about this error, try \PYG{l+s+sb}{`}rustc \PYGZhy{}\PYGZhy{}explain E0384\PYG{l+s+sb}{`}.
17+
error: could not compile \PYG{l+s+sb}{`}variables\PYG{l+s+sb}{`} due to previous error
18+
\end{Verbatim}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{p}{;}
4+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The value of x is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{p}{);}
5+
\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{6}\PYG{p}{;}
6+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The value of x is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{p}{);}
7+
\PYG{p}{\PYGZcb{}}
8+
\end{Verbatim}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{c+c1}{// addition}
4+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{sum}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{10}\PYG{p}{;}
5+
6+
\PYG{+w}{ }\PYG{c+c1}{// subtraction}
7+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{difference}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{95.5}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{l+m+mf}{4.3}\PYG{p}{;}
8+
9+
\PYG{+w}{ }\PYG{c+c1}{// multiplication}
10+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{product}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{4}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{l+m+mi}{30}\PYG{p}{;}
11+
12+
\PYG{+w}{ }\PYG{c+c1}{// division}
13+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{quotient}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{56.7}\PYG{+w}{ }\PYG{o}{/}\PYG{+w}{ }\PYG{l+m+mf}{32.2}\PYG{p}{;}
14+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{floored}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{+w}{ }\PYG{o}{/}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// Results in 0}
15+
16+
\PYG{+w}{ }\PYG{c+c1}{// remainder}
17+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{remainder}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{43}\PYG{+w}{ }\PYG{o}{\PYGZpc{}}\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{p}{;}
18+
\PYG{p}{\PYGZcb{}}
19+
\end{Verbatim}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{0.1}\PYG{p}{;}
4+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{y}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mf}{0.2}\PYG{p}{;}
5+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{z}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{y}\PYG{p}{;}
6+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}result: \PYGZob{}0\PYGZcb{}\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{z}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{//. 0.30000000000000004}
7+
\PYG{p}{\PYGZcb{}}
8+
\end{Verbatim}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{p}{;}
3+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{y}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{x}\PYG{p}{;}
4+
5+
\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}x = \PYGZob{}\PYGZcb{}, y = \PYGZob{}\PYGZcb{}\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{x}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{y}\PYG{p}{);}
6+
\end{Verbatim}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{5}\PYG{p}{;}
4+
5+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
6+
7+
\PYG{+w}{ }\PYG{p}{\PYGZob{}}
8+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{;}
9+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The value of x in the inner scope is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{p}{);}
10+
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
11+
12+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The value of x is: \PYGZob{}x\PYGZcb{}\PYGZdq{}}\PYG{p}{);}
13+
\PYG{p}{\PYGZcb{}}
14+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{l+m+mi}{57}\PYG{k}{u8}
3+
\end{Verbatim}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo run
3+
Compiling ownership v0.1.0 \PYG{o}{(}file:///projects/ownership\PYG{o}{)}
4+
error\PYG{o}{[}E0502\PYG{o}{]}: cannot borrow \PYG{l+s+sb}{`}s\PYG{l+s+sb}{`} as mutable because it is also borrowed as immutable
5+
\PYGZhy{}\PYGZhy{}\PYGZgt{} src/main.rs:6:14
6+
\PYG{p}{|}
7+
\PYG{l+m}{4} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{r1} \PYG{o}{=} \PYG{p}{\PYGZam{}}s\PYG{p}{;} // no problem
8+
\PYG{p}{|} \PYGZhy{}\PYGZhy{} immutable borrow occurs here
9+
\PYG{l+m}{5} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{r2} \PYG{o}{=} \PYG{p}{\PYGZam{}}s\PYG{p}{;} // no problem
10+
\PYG{l+m}{6} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{r3} \PYG{o}{=} \PYG{p}{\PYGZam{}}mut s\PYG{p}{;} // BIG PROBLEM
11+
\PYG{p}{|} \PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{} mutable borrow occurs here
12+
\PYG{l+m}{7} \PYG{p}{|}
13+
\PYG{l+m}{8} \PYG{p}{|} println!\PYG{o}{(}\PYG{l+s+s2}{\PYGZdq{}\PYGZob{}\PYGZcb{}, \PYGZob{}\PYGZcb{}, and \PYGZob{}\PYGZcb{}\PYGZdq{}}, r1, r2, r3\PYG{o}{)}\PYG{p}{;}
14+
\PYG{p}{|} \PYGZhy{}\PYGZhy{} immutable borrow later used here
15+
16+
For more information about this error, try \PYG{l+s+sb}{`}rustc \PYGZhy{}\PYGZhy{}explain E0502\PYG{l+s+sb}{`}.
17+
error: could not compile \PYG{l+s+sb}{`}ownership\PYG{l+s+sb}{`} due to previous error
18+
\end{Verbatim}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo run
3+
Compiling ownership v0.1.0 \PYG{o}{(}file:///projects/ownership\PYG{o}{)}
4+
error\PYG{o}{[}E0499\PYG{o}{]}: cannot borrow \PYG{l+s+sb}{`}s\PYG{l+s+sb}{`} as mutable more than once at a \PYG{n+nb}{time}
5+
\PYGZhy{}\PYGZhy{}\PYGZgt{} src/main.rs:5:14
6+
\PYG{p}{|}
7+
\PYG{l+m}{4} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{r1} \PYG{o}{=} \PYG{p}{\PYGZam{}}mut s\PYG{p}{;}
8+
\PYG{p}{|} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} first mutable borrow occurs here
9+
\PYG{l+m}{5} \PYG{p}{|} \PYG{n+nb}{let} \PYG{n+nv}{r2} \PYG{o}{=} \PYG{p}{\PYGZam{}}mut s\PYG{p}{;}
10+
\PYG{p}{|} \PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{}\PYGZca{} second mutable borrow occurs here
11+
\PYG{l+m}{6} \PYG{p}{|}
12+
\PYG{l+m}{7} \PYG{p}{|} println!\PYG{o}{(}\PYG{l+s+s2}{\PYGZdq{}\PYGZob{}\PYGZcb{}, \PYGZob{}\PYGZcb{}\PYGZdq{}}, r1, r2\PYG{o}{)}\PYG{p}{;}
13+
\PYG{p}{|} \PYGZhy{}\PYGZhy{} first borrow later used here
14+
15+
For more information about this error, try \PYG{l+s+sb}{`}rustc \PYGZhy{}\PYGZhy{}explain E0499\PYG{l+s+sb}{`}.
16+
error: could not compile \PYG{l+s+sb}{`}ownership\PYG{l+s+sb}{`} due to previous error
17+
\end{Verbatim}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{a}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{p}{[}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m+mi}{20}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m+mi}{30}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m+mi}{40}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m+mi}{50}\PYG{p}{];}
4+
5+
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{n}{element}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{n}{a}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
6+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}the value is: \PYGZob{}element\PYGZcb{}\PYGZdq{}}\PYG{p}{);}
7+
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
8+
\PYG{p}{\PYGZcb{}}
9+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
Rust is installed now. Great!
3+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{s}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{String}::\PYG{n}{from}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}hello\PYGZdq{}}\PYG{p}{);}
3+
\end{Verbatim}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{fn} \PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
3+
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{n}{number}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{..}\PYG{l+m+mi}{4}\PYG{p}{).}\PYG{n}{rev}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
4+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}\PYGZob{}number\PYGZcb{}!\PYGZdq{}}\PYG{p}{);}
5+
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
6+
\PYG{+w}{ }\PYG{n+nf+fm}{println!}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}LIFTOFF!!!\PYGZdq{}}\PYG{p}{);}
7+
\PYG{p}{\PYGZcb{}}
8+
\PYG{c+c1}{// output:}
9+
\PYG{c+c1}{// 3!}
10+
\PYG{c+c1}{// 2!}
11+
\PYG{c+c1}{// 1!}
12+
\PYG{c+c1}{// LIFTOFF!!!}
13+
\end{Verbatim}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} mkdir \PYGZti{}/projects
3+
\PYGZdl{} \PYG{n+nb}{cd} \PYGZti{}/projects
4+
\PYGZdl{} mkdir hello\PYGZus{}world
5+
\PYGZdl{} \PYG{n+nb}{cd} hello\PYGZus{}world
6+
\end{Verbatim}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k}{[package]}
3+
\PYG{n}{name}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}hello\PYGZus{}cargo\PYGZdq{}}
4+
\PYG{n}{version}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}0.1.0\PYGZdq{}}
5+
\PYG{n}{edition}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}2021\PYGZdq{}}
6+
7+
\PYG{c+c1}{\PYGZsh{} See more keys and their definitions at https://doc.rust\PYGZhy{}lang.org/cargo/reference/manifest.html}
8+
9+
\PYG{k}{[dependencies]}
10+
\end{Verbatim}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYGZdl{} cargo run
3+
Compiling ownership v0.1.0 \PYG{o}{(}file:///projects/ownership\PYG{o}{)}
4+
error\PYG{o}{[}E0106\PYG{o}{]}: missing lifetime specifier
5+
\PYGZhy{}\PYGZhy{}\PYGZgt{} src/main.rs:5:16
6+
\PYG{p}{|}
7+
\PYG{l+m}{5} \PYG{p}{|} fn dangle\PYG{o}{()} \PYGZhy{}\PYGZgt{} \PYG{p}{\PYGZam{}}String \PYG{o}{\PYGZob{}}
8+
\PYG{p}{|} \PYGZca{} expected named lifetime parameter
9+
\PYG{p}{|}
10+
\PYG{o}{=} help: this \PYG{k}{function}\PYG{l+s+s1}{\PYGZsq{}s return type contains a borrowed value, but there is no value for it to be borrowed from}
11+
\PYG{l+s+s1}{help: consider using the `\PYGZsq{}}static\PYG{l+s+sb}{`} lifetime
12+
\PYG{p}{|}
13+
\PYG{l+m}{5} \PYG{p}{|} fn dangle\PYG{o}{()} \PYGZhy{}\PYGZgt{} \PYG{p}{\PYGZam{}}\PYG{err}{\PYGZsq{}}static String \PYG{o}{\PYGZob{}}
14+
\PYG{p}{|} \PYGZti{}\PYGZti{}\PYGZti{}\PYGZti{}\PYGZti{}\PYGZti{}\PYGZti{}\PYGZti{}
15+
16+
For more information about this error, try \PYG{l+s+sb}{`}rustc \PYGZhy{}\PYGZhy{}explain E0106\PYG{l+s+sb}{`}.
17+
error: could not compile \PYG{l+s+sb}{`}ownership\PYG{l+s+sb}{`} due to previous error
18+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{p}{()}
3+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kt}{i32}
3+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
\PYG{k+kd}{let}\PYG{+w}{ }\PYG{n}{x}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mh}{0xff}
3+
\end{Verbatim}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
2+
cargo build \PYGZhy{}\PYGZhy{}release
3+
\end{Verbatim}

0 commit comments

Comments
 (0)