<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-07-09T03:50:02+00:00</updated><id>/feed.xml</id><title type="html">Your awesome title</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</subtitle><entry><title type="html">Welcome to Jekyll!</title><link href="/jekyll/update/2022/06/22/welcome-to-jekyll.html" rel="alternate" type="text/html" title="Welcome to Jekyll!" /><published>2022-06-22T07:37:17+00:00</published><updated>2022-06-22T07:37:17+00:00</updated><id>/jekyll/update/2022/06/22/welcome-to-jekyll</id><content type="html" xml:base="/jekyll/update/2022/06/22/welcome-to-jekyll.html">&lt;p&gt;You’ll find this post in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jekyll serve&lt;/code&gt;, which launches a web server and auto-regenerates your site when a file is updated.&lt;/p&gt;

&lt;p&gt;Jekyll requires blog post files to be named according to the following format:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YEAR-MONTH-DAY-title.MARKUP&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YEAR&lt;/code&gt; is a four-digit number, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MONTH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DAY&lt;/code&gt; are both two-digit numbers, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MARKUP&lt;/code&gt; is the file extension representing the format used in the file. After that, include the necessary front matter. Take a look at the source for this post to get an idea about how it works.&lt;/p&gt;

&lt;p&gt;Jekyll also offers powerful support for code snippets:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Tom'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; prints 'Hi, Tom' to STDOUT.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check out the &lt;a href=&quot;https://jekyllrb.com/docs/home&quot;&gt;Jekyll docs&lt;/a&gt; for more info on how to get the most out of Jekyll. File all bugs/feature requests at &lt;a href=&quot;https://github.com/jekyll/jekyll&quot;&gt;Jekyll’s GitHub repo&lt;/a&gt;. If you have questions, you can ask them on &lt;a href=&quot;https://talk.jekyllrb.com/&quot;&gt;Jekyll Talk&lt;/a&gt;.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.</summary></entry><entry><title type="html">BlockChain Learning Note</title><link href="/2022/06/21/block-chain-note.html" rel="alternate" type="text/html" title="BlockChain Learning Note" /><published>2022-06-21T00:00:00+00:00</published><updated>2022-06-21T00:00:00+00:00</updated><id>/2022/06/21/block-chain-note</id><content type="html" xml:base="/2022/06/21/block-chain-note.html">&lt;h1 id=&quot;区块链-blockchain&quot;&gt;区块链 &lt;strong&gt;BlockChain&lt;/strong&gt;&lt;/h1&gt;
&lt;h2 id=&quot;1-背景&quot;&gt;1 背景&lt;/h2&gt;
&lt;p&gt;现有的记录设备基本是&lt;strong&gt;中心化&lt;/strong&gt;的，无论是记账还是存储各种数据。即使如git也是将所有人的代码托管到服务器上进行存储，然后通过个人签名来提交修改。这样的做法已经非常成熟，并且看起来也足够稳定和安全。但是一旦服务器拥有者去篡改数据或者数据中心不幸被损毁，非中心的节点是无能为力的，我们只能选择相信这些服务器的拥有者是值得信任的。这当然已经足够好了，毕竟任何政府或者任何想要吸引人来使用的服务提供方都会极力维护这种稳定和秩序。&lt;/p&gt;
&lt;h2 id=&quot;2-基本设计思路&quot;&gt;2. 基本设计思路&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;区块链&lt;/strong&gt;则是为这种记录方式提供了一种新的形态——通过算法来保证记录的真实。也即无论谁发布了基于区块链的记录服务，将可以通过算法保证记录中的每项提交都真实可靠。只要这种算法还无破解方法，无论开发这套系统的人还是其他任何一个参与者，他们都将无法随意篡改和销毁数据。&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;去中心化&lt;/strong&gt;&lt;br /&gt;
首先需要改变的就是中心化的问题，因此区块链将所有的完整记录保存在每个参与者中，以下将参与者称为&lt;strong&gt;节点&lt;/strong&gt;，这样任何一个节点的数据被销毁都不会影响数据安全，除非所有节点都被摧毁。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;对提交进行签名&lt;/strong&gt;&lt;br /&gt;
由于每个节点都保管同一个账本，因此需要对修改的提交进行校验，否则每个节点都可以篡改。这里使用的方式是每个节点都有一对公/私钥。公钥作为其他人识别自己的标识是完全公开的，私钥作为证明身份的最终证据，因此需要各节点自行保密。每次提交修改时需要将私钥和信息进行联合并生成&lt;strong&gt;签名&lt;/strong&gt;，所有的节点收到这串签名后将与发出这串消息的节点公钥进行校对，从而保证这串消息由持有私钥者才能发出。
    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;generate_signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;private_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signature&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 生成签名
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;validate_signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;public_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 校验签名
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;加入自增计数 nonce&lt;/strong&gt;&lt;br /&gt;
现在虽然签名无法伪造，并且无法与任意其他信息搭配，但是签名和消息可以同时被复制，这也使得其他人可以反复发送同样的信息。因此需要加入自增计数，称为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nonce&lt;/code&gt;。这样其他人复制交易信息时将包含计次信息，即使多次重复发送也不会造成误解，因为此nonce的信息被记录后不会再记录。可以理解为：
    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;forge_commit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commit_message&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 生成提交信息
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;工作量证明 Proof of Work&lt;/strong&gt;&lt;br /&gt;
现在所有人从同一个数据源本开始，发生提交时节点会进行广播，其他节点接收到消息后进行校验和记录。可是所有人接收到消息的时间和顺序都是不一样的，如果需要保证各方记录的一致，则需要有个机制来确定谁可以进行记账。此处采用工作量证明。&lt;br /&gt;
每个人手里的全部记录（data）包含一个随机数，也叫nonce，不同于上面那个，这个数字随机而不是自增的。这个nonce目前只能通过暴力计算获得。计算的依据是将整个账本与nonce联合并做哈希（Hash）计算，如果结果的hash满足一定规则（比如前50位都是0，猜中的概率为1/2^50），则此nonce正确，从而具备进行本次记账的权力（并获得比特币，非必须）。每个区块包含上一个区块的hash值、数据记录和对应数据的nonce。代码描述如下：
    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;data_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;get_bitcoin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;write_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;有效区块链筛选&lt;/strong&gt;&lt;br /&gt;
如果有节点A需要按自己意图修改数据，需要计算出正确的nonce才能发布本区块，但是其他所有节点都在同时计算并且记录，而只要其他节点都不按照A节点发布的新区块续写的时候，A节点即使发布了新区块，区块曾长的速度也无法与其他统一的区块相比拟。而筛选有效区块链的方式就是看哪条链更长，并且超出一定长度。目前比特币交易需要约1小时等待有效链超出其他链6个区块。&lt;br /&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;em&gt;可是如果平台上交易甚少时，篡改者有充分时间计算新区块时该怎样呢？&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
因此对于篡改者来说，计算nonce并发布新区块后还需要继续维持本链的长度以成为有效链，而这则需要它能掌握整个系统中超过50%的算力才有可能做到。当然似乎反过来说，如果篡改链能超出正确链，则后续的节点也会无知地选择篡改链进行续写从而无从纠正。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;em&gt;而如果有多股势力参与篡改各自的版本时，是否只要成为掌握算力最多的一方即可？&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;3-公链与联盟链&quot;&gt;3. 公链与联盟链&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;公链&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;开源&lt;/li&gt;
  &lt;li&gt;无限制准入&lt;/li&gt;
  &lt;li&gt;业务方向丰富&lt;/li&gt;
  &lt;li&gt;升级困难&lt;/li&gt;
  &lt;li&gt;出错风险高而维护成本高&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;联盟链&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;非开源&lt;/li&gt;
  &lt;li&gt;限制CA证书准入&lt;/li&gt;
  &lt;li&gt;业务单一&lt;/li&gt;
  &lt;li&gt;升级相对可控&lt;/li&gt;
  &lt;li&gt;出错风险相对可控&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;4-波卡-polkadot&quot;&gt;4. 波卡 &lt;strong&gt;Polkadot&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;安全性、可扩展性与去中心化通常不可兼得，不同的区块链项目通常根据具体应用场景进行取舍。而波卡则主要有以下改进：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;将基于substrate的区块链全部挂载到同一个链上提高并行效率&lt;/li&gt;
  &lt;li&gt;挂载的链仍可以按自己需求去定制服务&lt;/li&gt;
  &lt;li&gt;波卡上的链之间有了共享信息和功能的可能，而不需要中心化的跨链服务商&lt;/li&gt;
  &lt;li&gt;允许波卡上的链开发者对自己的链进行维护和定制&lt;/li&gt;
  &lt;li&gt;无分叉升级，升级时不必维护两套交易历史来区分升级和未升级的节点&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;个人感想&quot;&gt;个人感想&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;是否可以&lt;strong&gt;异化节点&lt;/strong&gt;，而不是所有节点功能都一样，仿造社会机构设计一样，以使得不仅在算力上也在功能上互相制衡。&lt;/li&gt;
  &lt;li&gt;以前没有意识到中心化的节点也有资源的消耗，所以倾向于认为为了维护区块链去挖矿烧电毫无意义，但实际上中心化节点也为自身稳定构建了服务器，开发和行政人员，甚至可以说印刷代币或者铸造通过也是消耗自然资源的，这样比下来，电费和几块芯片可能还不算是很大的消耗。&lt;strong&gt;&lt;em&gt;不过这增大了各行各业的芯片荒可能是另一个需要讨论的话题&lt;/em&gt;&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;总的来说区块链给出了一种信任传递的方案，当我们没有坚不可摧的中心节点时，如何从平庸、自私的节点中构建信任。区块链给出的一个是权益的反馈以使大部分节点倾向于诚实，当大部分节点诚实时，做恶的难度则大大增加。而这需要消耗硬件成本和电费来维持，如果能稍微&lt;strong&gt;引入先验或者不仅在节点上传递信任并且累积信任&lt;/strong&gt;，比如惩罚在事后证明错误的链上续写的节点，是否是不错的设计。&lt;/li&gt;
&lt;/ol&gt;</content><author><name></name></author><category term="BlockChain" /><category term="LearningNote" /><category term="Chinese" /><summary type="html">区块链 BlockChain 1 背景 现有的记录设备基本是中心化的，无论是记账还是存储各种数据。即使如git也是将所有人的代码托管到服务器上进行存储，然后通过个人签名来提交修改。这样的做法已经非常成熟，并且看起来也足够稳定和安全。但是一旦服务器拥有者去篡改数据或者数据中心不幸被损毁，非中心的节点是无能为力的，我们只能选择相信这些服务器的拥有者是值得信任的。这当然已经足够好了，毕竟任何政府或者任何想要吸引人来使用的服务提供方都会极力维护这种稳定和秩序。 2. 基本设计思路 区块链则是为这种记录方式提供了一种新的形态——通过算法来保证记录的真实。也即无论谁发布了基于区块链的记录服务，将可以通过算法保证记录中的每项提交都真实可靠。只要这种算法还无破解方法，无论开发这套系统的人还是其他任何一个参与者，他们都将无法随意篡改和销毁数据。 去中心化 首先需要改变的就是中心化的问题，因此区块链将所有的完整记录保存在每个参与者中，以下将参与者称为节点，这样任何一个节点的数据被销毁都不会影响数据安全，除非所有节点都被摧毁。 对提交进行签名 由于每个节点都保管同一个账本，因此需要对修改的提交进行校验，否则每个节点都可以篡改。这里使用的方式是每个节点都有一对公/私钥。公钥作为其他人识别自己的标识是完全公开的，私钥作为证明身份的最终证据，因此需要各节点自行保密。每次提交修改时需要将私钥和信息进行联合并生成签名，所有的节点收到这串签名后将与发出这串消息的节点公钥进行校对，从而保证这串消息由持有私钥者才能发出。 generate_signature(private_key, message) -&amp;gt; signature # 生成签名 validate_signature(signature, public_key) -&amp;gt; True/False # 校验签名 加入自增计数 nonce 现在虽然签名无法伪造，并且无法与任意其他信息搭配，但是签名和消息可以同时被复制，这也使得其他人可以反复发送同样的信息。因此需要加入自增计数，称为nonce。这样其他人复制交易信息时将包含计次信息，即使多次重复发送也不会造成误解，因为此nonce的信息被记录后不会再记录。可以理解为： forge_commit(message, signature, nonce) -&amp;gt; commit_message # 生成提交信息 工作量证明 Proof of Work 现在所有人从同一个数据源本开始，发生提交时节点会进行广播，其他节点接收到消息后进行校验和记录。可是所有人接收到消息的时间和顺序都是不一样的，如果需要保证各方记录的一致，则需要有个机制来确定谁可以进行记账。此处采用工作量证明。 每个人手里的全部记录（data）包含一个随机数，也叫nonce，不同于上面那个，这个数字随机而不是自增的。这个nonce目前只能通过暴力计算获得。计算的依据是将整个账本与nonce联合并做哈希（Hash）计算，如果结果的hash满足一定规则（比如前50位都是0，猜中的概率为1/2^50），则此nonce正确，从而具备进行本次记账的权力（并获得比特币，非必须）。每个区块包含上一个区块的hash值、数据记录和对应数据的nonce。代码描述如下： while data_hash[:50] != [0] * 50: nonce = rand() data_hash = Hash(data, nonce) else: get_bitcoin() write_data() 有效区块链筛选 如果有节点A需要按自己意图修改数据，需要计算出正确的nonce才能发布本区块，但是其他所有节点都在同时计算并且记录，而只要其他节点都不按照A节点发布的新区块续写的时候，A节点即使发布了新区块，区块曾长的速度也无法与其他统一的区块相比拟。而筛选有效区块链的方式就是看哪条链更长，并且超出一定长度。目前比特币交易需要约1小时等待有效链超出其他链6个区块。 可是如果平台上交易甚少时，篡改者有充分时间计算新区块时该怎样呢？ 因此对于篡改者来说，计算nonce并发布新区块后还需要继续维持本链的长度以成为有效链，而这则需要它能掌握整个系统中超过50%的算力才有可能做到。当然似乎反过来说，如果篡改链能超出正确链，则后续的节点也会无知地选择篡改链进行续写从而无从纠正。 而如果有多股势力参与篡改各自的版本时，是否只要成为掌握算力最多的一方即可？</summary></entry><entry><title type="html">RUST Learning Note</title><link href="/2022/06/20/rust-note.html" rel="alternate" type="text/html" title="RUST Learning Note" /><published>2022-06-20T00:00:00+00:00</published><updated>2022-06-20T00:00:00+00:00</updated><id>/2022/06/20/rust-note</id><content type="html" xml:base="/2022/06/20/rust-note.html">&lt;h1 id=&quot;rust-learning-note&quot;&gt;RUST learning note&lt;/h1&gt;
&lt;h2 id=&quot;1-installation&quot;&gt;1. Installation&lt;/h2&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;--proto&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'=https'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--tlsv1&lt;/span&gt;.2 &lt;span class=&quot;nt&quot;&gt;-sSf&lt;/span&gt; https://sh.rustup.rs | sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Set default channel, ex. “stable”: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustup default stable&lt;/code&gt;&lt;br /&gt;
Update: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustup update&lt;/code&gt;&lt;br /&gt;
Uninstall Rust: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustup self uninstall&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&quot;2-data-type&quot;&gt;2. Data type&lt;/h2&gt;
&lt;p&gt;Data type should be defined when it’s build, unlike script languages.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Integer&lt;/strong&gt;: u8 -&amp;gt; i128&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Float&lt;/strong&gt;: f32, f64&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bool&lt;/strong&gt;: true, false. 0 cannot be taken as false.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Char&lt;/strong&gt;: ‘a’&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tuple&lt;/strong&gt;: (int, bool, char, float)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Array&lt;/strong&gt;: (int, int, …), (float, float, …)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vector&lt;/strong&gt;: Similar to Array, but length is changable&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HashMap(key, val)&lt;/strong&gt;: for storage&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;BTreeMap(key, val)&lt;/strong&gt;: can compare elements, less efficient when insert item&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HashSet(val)&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;BTreeSet(val)&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Enum: (val(Type))&lt;/strong&gt;, can assign defferent types for values
    &lt;h2 id=&quot;3-logic-control&quot;&gt;3. Logic control&lt;/h2&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;if, else if&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;match&lt;/strong&gt;
    &lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;zero&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;positive&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;negative&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;loop&lt;/strong&gt;: need “break;” to get out&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;while&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;for&lt;/strong&gt;: ex. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for num in 1..5&lt;/code&gt;, 5 is not iterated
    &lt;h2 id=&quot;4-exception&quot;&gt;4. Exception&lt;/h2&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Option&lt;/strong&gt;: check null pointer&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Result&lt;/strong&gt;: check return value of process&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;panic&lt;/strong&gt;
    &lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;panic&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;panic!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Un-solvable error&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;h2 id=&quot;5-cargo&quot;&gt;5. Cargo&lt;/h2&gt;
    &lt;p&gt;Cargo is a package management tool. Useful commands:&lt;/p&gt;
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Cargo new &lt;span class=&quot;nv&quot;&gt;$PROJECT_NAME&lt;/span&gt; &amp;lt;&lt;span class=&quot;nt&quot;&gt;--lib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
Cargo build &amp;lt;&lt;span class=&quot;nt&quot;&gt;--release&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
Cargo run
Cargo check     &lt;span class=&quot;c&quot;&gt;# check dependencies&lt;/span&gt;
Cargo &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;      &lt;span class=&quot;c&quot;&gt;# unit test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cargo.toml&lt;/strong&gt;
Toml file defines build content, like a cmake file
```toml
[package]
name = “example”
version = “0.1.0”
authors = [“someone &lt;a href=&quot;mailto:someone@someorg.com&quot;&gt;someone@someorg.com&lt;/a&gt;”]
edition = “2022”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;[[bin]]
name = “example_bin”
path = “src/main.rs”&lt;/p&gt;

&lt;p&gt;[dependencies]
…
```&lt;/p&gt;</content><author><name></name></author><category term="Rust" /><category term="Coding" /><category term="LearningNote" /><category term="English" /><summary type="html">RUST learning note 1. Installation curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh Set default channel, ex. “stable”: rustup default stable Update: rustup update Uninstall Rust: rustup self uninstall 2. Data type Data type should be defined when it’s build, unlike script languages. Integer: u8 -&amp;gt; i128 Float: f32, f64 Bool: true, false. 0 cannot be taken as false. Char: ‘a’ Tuple: (int, bool, char, float) Array: (int, int, …), (float, float, …) Vector: Similar to Array, but length is changable HashMap(key, val): for storage BTreeMap(key, val): can compare elements, less efficient when insert item HashSet(val) BTreeSet(val) Enum: (val(Type)), can assign defferent types for values 3. Logic control if, else if match match a { 0 =&amp;gt; println!(&quot;zero&quot;) 1..=i32::MAX =&amp;gt; println!(&quot;positive&quot;) _ =&amp;gt; println!(&quot;negative&quot;) } loop: need “break;” to get out while for: ex. for num in 1..5, 5 is not iterated 4. Exception Option: check null pointer Result: check return value of process panic use std::panic panic!(&quot;Un-solvable error&quot;) 5. Cargo Cargo is a package management tool. Useful commands: Cargo new $PROJECT_NAME &amp;lt;--lib&amp;gt; Cargo build &amp;lt;--release&amp;gt; Cargo run Cargo check # check dependencies Cargo test # unit test Cargo.toml Toml file defines build content, like a cmake file ```toml [package] name = “example” version = “0.1.0” authors = [“someone someone@someorg.com”] edition = “2022”</summary></entry></feed>