<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Kaifeng Zheng</title>
    <subtitle>Some notes on Rust, distributed systems, databases, and more.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://kev1n8.github.io/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://kev1n8.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-04-01T00:00:00+00:00</updated>
    <id>https://kev1n8.github.io/atom.xml</id>
    <entry xml:lang="en">
        <title>OpenTelemetry Basics Tour</title>
        <published>2025-04-01T00:00:00+00:00</published>
        <updated>2025-04-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/opentelemetry-basics-tour/"/>
        <id>https://kev1n8.github.io/posts/opentelemetry-basics-tour/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/opentelemetry-basics-tour/">&lt;h1 id=&quot;opentelemetry-basics-tour&quot;&gt;OpenTelemetry Basics Tour&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;&#x2F;h2&gt;
&lt;p&gt;OpenTelemetry is an observability framework and toolkit to &lt;strong&gt;create&lt;&#x2F;strong&gt; and &lt;strong&gt;manage&lt;&#x2F;strong&gt; telemetry data like &lt;strong&gt;traces&lt;&#x2F;strong&gt;, &lt;strong&gt;metrics&lt;&#x2F;strong&gt;, and &lt;strong&gt;logs&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;concepts&quot;&gt;Concepts&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;traces&quot;&gt;Traces&lt;&#x2F;h4&gt;
&lt;p&gt;Composed of spans in a tree structure. A trace represents the end-to-end journey of a request, while a span represents a single operation within that journey.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;TracerProvider&lt;&#x2F;code&gt; is a factory for &lt;code&gt;Tracer&lt;&#x2F;code&gt;s. A &lt;code&gt;Tracer&lt;&#x2F;code&gt; creates spans with information about what is happening for a given operation.
&lt;code&gt;Trace Exporter&lt;&#x2F;code&gt; sends traces to a consumer, which can be standard output, the OpenTelemetry Collector, or any open source or vendor backend.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;spans&quot;&gt;Spans&lt;&#x2F;h5&gt;
&lt;p&gt;Fields contained:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Name&lt;&#x2F;li&gt;
&lt;li&gt;Parent span ID (empty for root spans)&lt;&#x2F;li&gt;
&lt;li&gt;Start and End Timestamps&lt;&#x2F;li&gt;
&lt;li&gt;Span Context&lt;&#x2F;li&gt;
&lt;li&gt;Attributes — info in KV pairs&lt;&#x2F;li&gt;
&lt;li&gt;Span Events — things happening at a point in time, whose timestamp is meaningful&lt;&#x2F;li&gt;
&lt;li&gt;Span Links — indicate causal relationships between spans in distributed situations&lt;&#x2F;li&gt;
&lt;li&gt;Span Status — indicates whether the tracked operation completed with or without error (&lt;code&gt;Unset&lt;&#x2F;code&gt;, &lt;code&gt;Error&lt;&#x2F;code&gt;), or was explicitly set to &lt;code&gt;Ok&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Kind:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Client — remote call such as an outgoing HTTP request or DB call&lt;&#x2F;li&gt;
&lt;li&gt;Server — incoming remote call such as an incoming HTTP request or RPC&lt;&#x2F;li&gt;
&lt;li&gt;Internal — operations that do not cross a process boundary, such as instrumenting a function call&lt;&#x2F;li&gt;
&lt;li&gt;Producer — represents the creation of a job&lt;&#x2F;li&gt;
&lt;li&gt;Consumer — represents the processing of a job, after the Producer span has already ended&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;metrics&quot;&gt;Metrics&lt;&#x2F;h4&gt;
&lt;p&gt;A &lt;strong&gt;metric&lt;&#x2F;strong&gt; is a measurement of a service captured at runtime. The moment of capturing a measurement is known as a &lt;strong&gt;metric event&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Application and request metrics are important indicators of availability and performance.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;MeterProvider&lt;&#x2F;code&gt; is a factory for &lt;code&gt;Meter&lt;&#x2F;code&gt;s. A &lt;code&gt;Meter&lt;&#x2F;code&gt; creates metric instruments, capturing measurements about a service at runtime. &lt;code&gt;Metric Exporter&lt;&#x2F;code&gt;s send metric data to a consumer (similarly to &lt;code&gt;Trace Exporter&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;h5 id=&quot;metric-instruments&quot;&gt;Metric Instruments&lt;&#x2F;h5&gt;
&lt;p&gt;Fields:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Name&lt;&#x2F;li&gt;
&lt;li&gt;Kind&lt;&#x2F;li&gt;
&lt;li&gt;Unit (optional)&lt;&#x2F;li&gt;
&lt;li&gt;Description (optional)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Name, unit, and description are chosen by the developer or defined via &lt;strong&gt;semantic conventions&lt;&#x2F;strong&gt; for common ones like request and process metrics.&lt;&#x2F;p&gt;
&lt;p&gt;Kind enums:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Counter — a value accumulating over time&lt;&#x2F;li&gt;
&lt;li&gt;Asynchronous Counter — same, but is collected &lt;strong&gt;once&lt;&#x2F;strong&gt; for the export&lt;&#x2F;li&gt;
&lt;li&gt;UpDownCounter — counter that can go down&lt;&#x2F;li&gt;
&lt;li&gt;Asynchronous UpDownCounter&lt;&#x2F;li&gt;
&lt;li&gt;Gauge&lt;&#x2F;li&gt;
&lt;li&gt;Histogram&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h5 id=&quot;aggregation&quot;&gt;Aggregation&lt;&#x2F;h5&gt;
&lt;p&gt;Exact or estimated statistics about metric events during a time window.&lt;&#x2F;p&gt;
&lt;p&gt;e.g.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Total bytes read by a service, per protocol type&lt;&#x2F;li&gt;
&lt;li&gt;Total bytes read and bytes per request&lt;&#x2F;li&gt;
&lt;li&gt;Duration of a system call&lt;&#x2F;li&gt;
&lt;li&gt;Request sizes for trend prediction&lt;&#x2F;li&gt;
&lt;li&gt;CPU or memory usage of a process&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;logs&quot;&gt;Logs&lt;&#x2F;h4&gt;
&lt;p&gt;A log is a timestamped text record.&lt;&#x2F;p&gt;
&lt;p&gt;Log Appender&#x2F;Bridge emits logs into an OpenTelemetry &lt;code&gt;LogRecordExporter&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;LoggerProvider&lt;&#x2F;code&gt; is a factory for &lt;code&gt;Logger&lt;&#x2F;code&gt;s. A &lt;code&gt;Logger&lt;&#x2F;code&gt; creates log records. &lt;code&gt;LogRecordExporter&lt;&#x2F;code&gt;s send log records to a consumer.&lt;&#x2F;p&gt;
&lt;p&gt;Fields:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Timestamp — Time when the event occurred&lt;&#x2F;li&gt;
&lt;li&gt;ObservedTimestamp — Time when the event was observed&lt;&#x2F;li&gt;
&lt;li&gt;TraceId — Request trace ID&lt;&#x2F;li&gt;
&lt;li&gt;SpanId — Request span ID&lt;&#x2F;li&gt;
&lt;li&gt;TraceFlags — W3C trace flag&lt;&#x2F;li&gt;
&lt;li&gt;SeverityText — The severity text (also known as log level)&lt;&#x2F;li&gt;
&lt;li&gt;SeverityNumber — Numerical value of the severity&lt;&#x2F;li&gt;
&lt;li&gt;Body — The body of the log record&lt;&#x2F;li&gt;
&lt;li&gt;Resource — Describes the source of the log&lt;&#x2F;li&gt;
&lt;li&gt;InstrumentationScope — Describes the scope that emitted the log&lt;&#x2F;li&gt;
&lt;li&gt;Attributes — Additional information about the event&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;major-components&quot;&gt;Major Components&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;A specification for all components&lt;&#x2F;li&gt;
&lt;li&gt;A standard protocol that defines the shape of telemetry data&lt;&#x2F;li&gt;
&lt;li&gt;Semantic conventions that define a standard naming scheme for common telemetry data types&lt;&#x2F;li&gt;
&lt;li&gt;APIs that define how to generate telemetry data&lt;&#x2F;li&gt;
&lt;li&gt;Language SDKs that implement the specification, APIs, and export of telemetry data&lt;&#x2F;li&gt;
&lt;li&gt;A library ecosystem that implements instrumentation for common libraries and frameworks&lt;&#x2F;li&gt;
&lt;li&gt;Automatic instrumentation components that generate telemetry data without requiring code changes&lt;&#x2F;li&gt;
&lt;li&gt;The OpenTelemetry Collector, a proxy that receives, processes, and exports telemetry data&lt;&#x2F;li&gt;
&lt;li&gt;Other tools&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Rust WASM: Building Conway&#x27;s Game of Life</title>
        <published>2025-04-01T00:00:00+00:00</published>
        <updated>2025-04-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/rust-wasm-building-conway-s-game-of-life/"/>
        <id>https://kev1n8.github.io/posts/rust-wasm-building-conway-s-game-of-life/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/rust-wasm-building-conway-s-game-of-life/">&lt;h1 id=&quot;rust-wasm-building-conway-s-game-of-life&quot;&gt;Rust WASM: Building Conway&#x27;s Game of Life&lt;&#x2F;h1&gt;
&lt;p&gt;I&#x27;ve heard of wasm for quite a while, and finally decided to try it out.&lt;&#x2F;p&gt;
&lt;p&gt;This blog briefly writes the process of the practice.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;&#x2F;h2&gt;
&lt;p&gt;Should know basic web development stuff, since wasm is run inside a browser (mostly).&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;HTML&lt;&#x2F;li&gt;
&lt;li&gt;CSS&lt;&#x2F;li&gt;
&lt;li&gt;JavaScript&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;There are technically 2 ways of using rust wasm.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Build the whole project in Rust, or&lt;&#x2F;li&gt;
&lt;li&gt;Rust the heavy part of the project and use it as a lib.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Currently the latter one is preferred.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;game-of-life&quot;&gt;Game of Life&lt;&#x2F;h2&gt;
&lt;p&gt;The practice is going to focus on implementing Conway&#x27;s Game of Life in Rust, building it to wasm and use it in JS.&lt;&#x2F;p&gt;
&lt;p&gt;The rules of the game are:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Any live cell with fewer than two live neighbours dies, as if by underpopulation.&lt;&#x2F;li&gt;
&lt;li&gt;Any live cell with two or three live neighbours lives on to the next generation.&lt;&#x2F;li&gt;
&lt;li&gt;Any live cell with more than three live neighbours dies, as if by overpopulation.&lt;&#x2F;li&gt;
&lt;li&gt;Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;For more info: https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Conway%27s_Game_of_Life&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;rust-part&quot;&gt;Rust Part&lt;&#x2F;h3&gt;
&lt;p&gt;Rust comes with powerful tools like &lt;code&gt;wasm-pack&lt;&#x2F;code&gt;, &lt;code&gt;wasm-bindgen&lt;&#x2F;code&gt; and more for wasm programming.&lt;&#x2F;p&gt;
&lt;p&gt;We use &lt;code&gt;#[wasm-bindgen]&lt;&#x2F;code&gt; macro to make complier generates wasm-related files (js API file, wasm memory file, ts file and so on) when we use &lt;code&gt;wasm-pack build&lt;&#x2F;code&gt; to build our rust wasm lib.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll simply copy the code as follows:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; fixedbitset&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;FixedBitSet&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; wasm_bindgen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;prelude&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::*&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[wasm_bindgen]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[repr(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Clone&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Copy&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Debug&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; PartialEq&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Eq&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Dead&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Alive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[wasm_bindgen]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; FixedBitSet&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; get_index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; * self.&lt;&#x2F;span&gt;&lt;span&gt;width &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; live_neighbor_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u8&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;height &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;] {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;width &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;] {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    continue&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; neighbor_row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_row&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; % self.&lt;&#x2F;span&gt;&lt;span&gt;height;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; neighbor_col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;column&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta_col&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; % self.&lt;&#x2F;span&gt;&lt;span&gt;width;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;neighbor_row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; neighbor_col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; += self.&lt;&#x2F;span&gt;&lt;span&gt;cells[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u8&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        count&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[wasm_bindgen]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 64&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 64&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sz&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; FixedBitSet&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_capacity&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;sz&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;sz&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; js_sys&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;() &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            width&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            height&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            cells&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;width&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;height&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; cells&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_slice&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ptr&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; tick&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..self.&lt;&#x2F;span&gt;&lt;span&gt;height {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..self.&lt;&#x2F;span&gt;&lt;span&gt;width {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;cells[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; live_neigbors&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;live_neighbor_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                next&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; match&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; live_neigbors&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;otherwise&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; otherwise&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;cells &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;javascript-part&quot;&gt;JavaScript Part&lt;&#x2F;h3&gt;
&lt;p&gt;With the functional lib we built, now all we have to do is to use it to display the animation on the browser.&lt;&#x2F;p&gt;
&lt;p&gt;A basic web app is consist of:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;HTML: Defining the layout of the website&lt;&#x2F;li&gt;
&lt;li&gt;CSS: Defining the style of the website&lt;&#x2F;li&gt;
&lt;li&gt;JavaScript: Handling any user input and logical changes.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;For HTML and CSS:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;html&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DOCTYPE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D19A66;&quot;&gt; html&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;html&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;head&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D19A66;&quot;&gt; charset&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Hello wasm-pack!&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;head&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;canvas&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D19A66;&quot;&gt; id&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;game-of-life-canvas&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;canvas&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D19A66;&quot;&gt; src&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;.&#x2F;bootstrap.js&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;style&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    body&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      position&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; absolute&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      top&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      left&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      width&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;%&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      height&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;%&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      display&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; flex&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      flex-direction&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      align-items&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; center&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;      justify-content&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; center&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;style&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;html&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For JS:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; from&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;wasm-game-of-life&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Import the linear memory space of wasm.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; memory&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; from&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;wasm-game-of-life&#x2F;wasm_game_of_life_bg.wasm&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; px&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; GRID_COLOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;#CCCCCC&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; DEAD_COLOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;#FFFFFF&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ALIVE_COLOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;#000000&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Construct the universe, and get its width and height.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Give the canvas room for all of our cells and a 1px border&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; around each of them.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvas&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; document&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;game-of-life-canvas&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;getContext&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;2d&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; renderLoop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;tick&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    drawGrid&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    drawCells&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    requestAnimationFrame&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;renderLoop&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; drawGrid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;beginPath&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;strokeStyle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; GRID_COLOR&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Vertical lines.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;moveTo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lineTo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;, (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Horizontal lines.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;moveTo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lineTo&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;stroke&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; getIndex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; column&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; drawCells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Accessing the buffer of wasm.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cellsPtr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;cells&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Uint8Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;memory&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cellsPtr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;beginPath&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; getIndex&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;fillStyle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cells&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ===&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Cell&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Dead&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                ?&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; DEAD_COLOR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ALIVE_COLOR&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;fillRect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                CELL_SIZE&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                CELL_SIZE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ctx&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;stroke&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;drawGrid&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;drawCells&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;requestAnimationFrame&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;renderLoop&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;&#x2F;h2&gt;
&lt;p&gt;Example test.rs:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Only test for target: &amp;quot;wasm-unknown-unknown&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#![cfg(target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;wasm32&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#![cfg(test)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Use wasm_bindgen_test and our [`Universe`];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; wasm_bindgen_test&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::*&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; wasm_game_of_life&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Universe&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Configure our test to run in the browser.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm_bindgen_test_configure!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;run_in_browser&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; input_spaceship&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_width&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_height&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_cells&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; expected_spaceship&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_width&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_height&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_cells&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Similar to #[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[wasm_bindgen_test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; test_tick&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input_universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; input_spaceship&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; expected_universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; expected_spaceship&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    input_universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;tick&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input_universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_cells&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; expected_universe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_cells&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By setting the test rs file, we can simply &lt;code&gt;wasm-pack test --chrome --headless&lt;&#x2F;code&gt; to test our test suite.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;--chrome&lt;&#x2F;code&gt; means we prefer to run the test in Google Chrome, so be sure the latest Google Chrome is downloaded.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;--headless&lt;&#x2F;code&gt; means we prefer to test without opening the browser and viewing.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;debugging&quot;&gt;Debugging&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;log-for-panics&quot;&gt;Log for Panics&lt;&#x2F;h3&gt;
&lt;p&gt;If our rust wasm panics for unknown reasons, we would like to at least be aware of the location of panicking.&lt;&#x2F;p&gt;
&lt;p&gt;We could achieve this by using the crate &lt;code&gt;console_error_panic_hook&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Usage is simply:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Place this in the init function.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; `set_once` ensure `set_hook` is called once.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;console_error_panic_hook&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_once&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, if the rust part panics, we&#x27;ll get messages like (build with &lt;code&gt;wasm-pack build --dev&lt;&#x2F;code&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;panicked at src&#x2F;lib.rs:65:9:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;explicit panic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Stack:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;logError@&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[__wbg_new_8a6f238a6ece86ea externref shim]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[console_error_panic_hook::Error::new::h19e56b4cb73b36b1]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[console_error_panic_hook::hook_impl::hbfe233a683db229c]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[console_error_panic_hook::hook::h9ea87a8daf4cfd59]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[core::ops::function::Fn::call::h9d1dc855776bf28e]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[std::panicking::rust_panic_with_hook::h9ea01914b5d92659]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[std::panicking::begin_panic::{{closure}}::h1965b49f537d1c88]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[std::sys::backtrace::__rust_end_short_backtrace::hdeabdd4295ec7969]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[std::panicking::begin_panic::hbacf3f38466eeebe]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[wasm_game_of_life::Universe::new::hc5d33d50da932d85]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wasm_game_of_life.wasm.wasm-function[universe_new]@[wasm code]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;new@&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;log-error-messages-to-console&quot;&gt;Log Error Messages to Console&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s useful to log info if something went wrong. How do we log then?&lt;&#x2F;p&gt;
&lt;p&gt;We need another dependency, &lt;code&gt;web-sys&lt;&#x2F;code&gt;, which provides us the &lt;code&gt;console::log&lt;&#x2F;code&gt; API.&lt;&#x2F;p&gt;
&lt;p&gt;Well, simple as that. Then we can write a macro to easy up the coding:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;macro_rules! log&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; $&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;tt&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        web_sys&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;log_1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; $&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Log while ticking.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;log!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;cell[&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;] is initially &lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; and has &lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; live neighbors&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    col&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    cell&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    live_neigbors&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;use-a-debugger&quot;&gt;Use a Debugger&lt;&#x2F;h3&gt;
&lt;p&gt;Either by placing an &lt;code&gt;debugger&lt;&#x2F;code&gt; in advanced in &lt;code&gt;.js&lt;&#x2F;code&gt; or by clicking in the sources interface of the browser is ok for js.&lt;&#x2F;p&gt;
&lt;p&gt;However, there&#x27;s currently no mature debugging tool for rust wasm. We may end up stepping into raw wasm instructions rather than the Rust source text we authored.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;interaction-between-rust-and-js&quot;&gt;Interaction between Rust and JS&lt;&#x2F;h2&gt;
&lt;p&gt;We are going to add 2 interactions:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Play and pause button, and&lt;&#x2F;li&gt;
&lt;li&gt;Toggle cell state by clicking&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;add-a-button&quot;&gt;Add a Button&lt;&#x2F;h3&gt;
&lt;p&gt;Add a button in &lt;code&gt;.html&lt;&#x2F;code&gt; with &lt;code&gt;&amp;lt;button id=&quot;play-pause&quot;&amp;gt;&amp;lt;&#x2F;button&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;index.js&lt;&#x2F;code&gt;, add the logic code:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; playPauseButton&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; document&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;play-pause&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; isPaused&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; animationId&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ===&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; null&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; play&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    playPauseButton&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;textContent&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;⏸&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    renderLoop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; pause&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    playPauseButton&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;textContent&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;▶&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    cancelAnimationFrame&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;animationId&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    animationId&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; null&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; This would be set in `renderLoop`.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;playPauseButton&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;addEventListener&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;click&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; event&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;isPaused&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        play&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        pause&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;});&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;toggling-cells&quot;&gt;Toggling Cells&lt;&#x2F;h3&gt;
&lt;p&gt;First we have to impl the toggle method on the rust side.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Universe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; toggle_cell&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; !self.&lt;&#x2F;span&gt;&lt;span&gt;cells[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, we add the toggling logic code in js.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;addEventListener&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;click&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; event&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; boundingRect&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;getBoundingClientRect&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; scaleX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; boundingRect&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; scaleY&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvas&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; boundingRect&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvasLeft&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;event&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;clientX&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; boundingRect&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;left&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; scaleX&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; canvasTop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;event&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;clientY&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; boundingRect&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;top&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; scaleY&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;canvasTop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; height&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;canvasLeft&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;CELL_SIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; width&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    universe&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;toggle_cell&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    drawCells&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;})&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;time-profiling&quot;&gt;Time Profiling&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;counting-fps-by-performance-now&quot;&gt;Counting FPS by &lt;code&gt;performance.now()&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;Add dependency &lt;code&gt;web-sys&lt;&#x2F;code&gt; with features &lt;code&gt;[&quot;Window&quot;, &quot;Performance&quot;]&lt;&#x2F;code&gt; if would like to count fps in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;Or we can simply count it in js:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; fps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; class&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    constructor&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;fps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; document&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;fps&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;lastFrameTimeStamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; performance&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;now&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    render&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; now&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; performance&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;now&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; now&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; - this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;lastFrameTimeStamp&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;lastFrameTimeStamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; now&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; fps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1000&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;fps&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;shift&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; min&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; Infinity&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;Infinity&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt; this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            sum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; += this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            min&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; min&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;max&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; mean&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F; this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;frames&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;fps&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;textContent&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; `&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Frames per Second:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;         latest = &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;round&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;fps&lt;&#x2F;span&gt;&lt;span&gt;)}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;avg of last 100 = &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;round&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span&gt;)}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;min of last 100 = &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;round&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;)}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;max of last 100 = &lt;&#x2F;span&gt;&lt;span&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;round&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;max&lt;&#x2F;span&gt;&lt;span&gt;)}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;`.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;trim&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And call &lt;code&gt;fps.render()&lt;&#x2F;code&gt; in &lt;code&gt;renderLoop&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;set-timer-for-universe-tick&quot;&gt;Set Timer for &lt;code&gt;Universe::tick&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        console&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;time_with_label&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Drop&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; drop&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        console&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;time_end_with_label&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By that the timer ticking will show outputs in the js console.&lt;&#x2F;p&gt;
&lt;p&gt;(TODO: Does it brings more info in the recorded frames(waterfalls)?)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;enhancing-performance&quot;&gt;Enhancing Performance&lt;&#x2F;h3&gt;
&lt;p&gt;With the timer above, we can inspect the time cost of each function calls.&lt;&#x2F;p&gt;
&lt;p&gt;For some reason my bottleneck is different from the project guide, but I&#x27;ll first enhance the render part to keep the same with the guide anyway, which is to skip &lt;code&gt;fillRect&lt;&#x2F;code&gt; when it&#x27;s not necessary.&lt;&#x2F;p&gt;
&lt;p&gt;The bottleneck of my implementation is &lt;code&gt;Universe::tick&lt;&#x2F;code&gt;. Let&#x27;s add more timer in between:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; tick&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; times&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;allocate next cells&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;cells&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;times&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;new_genaration&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..self.&lt;&#x2F;span&gt;&lt;span&gt;height {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..self.&lt;&#x2F;span&gt;&lt;span&gt;width {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;cells[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; live_neigbors&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;live_neighbor_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next_cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = match&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; live_neigbors&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; true&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;otherwise&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; otherwise&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    next&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next_cell&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;free old cells&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;cells &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;TODO: for some reason I don&#x27;t see the timer with these labels in the dev tools.&lt;&#x2F;p&gt;
&lt;p&gt;Any way, according to the flamegraph, the most time consuming function call is &lt;code&gt;Universe::live_neighbor_count&lt;&#x2F;code&gt; (check out the orange bar in the pic below).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;kev1n8.github.io&#x2F;posts&#x2F;rust-wasm-building-conway-s-game-of-life&#x2F;.&#x2F;Task%20Archive&#x2F;images&#x2F;wasm%20count%20neighbor%20flamegraph.png&quot; alt=&quot;Flamegraph showing the time-consuming function call in Universe::live_neighbor_count&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;shrinking-binary-size&quot;&gt;Shrinking Binary Size&lt;&#x2F;h2&gt;
&lt;p&gt;Use &lt;code&gt;wasm-opt&lt;&#x2F;code&gt; tool to shrink code size:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;# Optimize for size.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-opt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; -Os -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; output.wasm input.wasm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;# Optimize aggressively for size.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-opt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; -Oz -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; output.wasm input.wasm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;# Optimize for speed.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-opt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; -O -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; output.wasm input.wasm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;# Optimize aggressively for speed.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-opt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; -O3 -o&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; output.wasm input.wasm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For further optimizations, we could turn our &lt;code&gt;Universe&lt;&#x2F;code&gt; into a static global instance, and so is its cells. Without any allocator, we could even make our lib &lt;code&gt;#![no_std]&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll leave it here, though.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;publishing&quot;&gt;Publishing&lt;&#x2F;h2&gt;
&lt;p&gt;Publish to npm:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-pack&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; login&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-pack&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; build&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;wasm-pack&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; publish&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Understanding Async Rust: The Future Trait</title>
        <published>2025-04-01T00:00:00+00:00</published>
        <updated>2025-04-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/understanding-async-rust-the-future-trait/"/>
        <id>https://kev1n8.github.io/posts/understanding-async-rust-the-future-trait/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/understanding-async-rust-the-future-trait/">&lt;h1 id=&quot;understanding-async-rust-the-future-trait&quot;&gt;Understanding Async Rust: The Future Trait&lt;&#x2F;h1&gt;
&lt;p&gt;This blog is intended to summarize the key ideas when it comes to async Rust, hence it&#x27;s not going to deep in every topic. Please refer to more professional papers.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;general-picture-of-async-and-await&quot;&gt;General Picture of &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s obvious that the biggest difference between sync and async code is that the latter one has these keywords used in function signatures or bodies. However, one may wonder what is that supposed to mean.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;async&lt;&#x2F;code&gt; is used in front of a &lt;code&gt;fn&lt;&#x2F;code&gt; or a closure. It literally means that the &lt;code&gt;fn&lt;&#x2F;code&gt; or the closure is returning a &lt;code&gt;Future&lt;&#x2F;code&gt;. Meanwhile, &lt;code&gt;await&lt;&#x2F;code&gt; is &quot;called&quot; on a &lt;code&gt;Future&lt;&#x2F;code&gt;. To simply say, it tells the compiler to generate necessary codes for state changing.&lt;&#x2F;p&gt;
&lt;p&gt;So the big picture is, we treat async functions (or async closures) as a mission to be completed for in the future. And it&#x27;s not going to activate&#x2F;be invoked unless we call &lt;code&gt;await&lt;&#x2F;code&gt; on that &lt;code&gt;Future&lt;&#x2F;code&gt;. While waiting for the result of that &lt;code&gt;Future&lt;&#x2F;code&gt;, the control is yielded to other tasks that might be ready and waiting to be executed. The &quot;pause&quot; and &quot;resume&quot; of tasks mechanism should thanks to a specific async task runtime.&lt;&#x2F;p&gt;
&lt;p&gt;There are several great tools when it comes to async runtime for Rust, e.g. tokio, async-std, smol. Since tokio is the most used one, we&#x27;ll focus on it.&lt;&#x2F;p&gt;
&lt;p&gt;A runtime is responsible for the reconsiling of tasks. In tokio, there are several workers, each runs on a thread, polling the futures (we&#x27;ll explain &quot;polling&quot; later. you may think it&#x27;s like processing that task). Each worker has a local queue whose items are tasks waiting to be polled. And there also is a global queue first. The task is stealing-based in tokio, which means if a worker has empty queue, it&#x27;ll try to steal task from other workers&#x27; queue if available.&lt;&#x2F;p&gt;
&lt;p&gt;A simple example of usage:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; run_async_task&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;main]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; fut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; run_async_task&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; Return a future, not run.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; fut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; Here, the control yields and the runtime will start `run_async_task()`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;about-future&quot;&gt;About &lt;code&gt;Future&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;Future&lt;&#x2F;code&gt; is a trait that indicates its result might be ready in the future. It&#x27;s similar to promise in JavaScript. Let&#x27;s look at its definition:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; The type of value produced on completion.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Output&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; poll&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pin&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Context&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;example&quot;&gt;Example&lt;&#x2F;h2&gt;
&lt;p&gt;By the following example, you&#x27;ll get how futrues work:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; fn_1&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;    1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; add_2&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; run_add&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; fn_1&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; add_2&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;   println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;main]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    run_add&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Simplified pseudocode illustrating the state machine for run_add()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Pin&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;task&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Context&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Waker&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Let&amp;#39;s assume these async functions exist:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; fn_1&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;    1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; Returns immediately for simplicity.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; add_2&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; Also returns immediately for simplicity.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; The state machine states for run_add&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Start&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                   &#x2F;&#x2F; Before any await.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    WaitingFn1&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;              &#x2F;&#x2F; Waiting for fn_1 to complete.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pin&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    WaitingAdd2&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;      &#x2F;&#x2F; Holds the result from fn_1.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pin&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Finished&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAdd&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAdd&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; poll&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pin&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Context&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;()&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        loop&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            match &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;state {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; We initiate fn_1() here.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future_fn1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;fn_1&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; Transition state to WaitingFn1 with the future.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    self.&lt;&#x2F;span&gt;&lt;span&gt;state &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;WaitingFn1&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future_fn1&lt;&#x2F;span&gt;&lt;span&gt; };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;WaitingFn1&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; Poll the inner future from fn_1.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_mut&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cx&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                        Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Ready&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; fn_1() is done, we got the result &amp;#39;a&amp;#39;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; Now prepare to call add_2(a).&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future_add2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;add_2&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            self.&lt;&#x2F;span&gt;&lt;span&gt;state &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;WaitingAdd2&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future_add2&lt;&#x2F;span&gt;&lt;span&gt; };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                        Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Pending&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; fn_1() isn&amp;#39;t finished yet.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; Yield control back to the executor.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Pending&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;WaitingAdd2&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; Poll the inner future from add_2.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; future&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_mut&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cx&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                        Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Ready&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; add_2 has finished. We can now use the result.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                            println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Result: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            self.&lt;&#x2F;span&gt;&lt;span&gt;state &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Finished&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Ready&lt;&#x2F;span&gt;&lt;span&gt;(());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                        Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Pending&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                            &#x2F;&#x2F; Still waiting for add_2—for now, yield control.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Pending&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Finished&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                    panic!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Polling after completion&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;main]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    RunAdd&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RunAddState&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Start&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; Runtime polls here.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, the async runtime will start &quot;epolling&quot; (kinda) the futures, until it returns &lt;code&gt;Finished&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;ref. https:&#x2F;&#x2F;wiki.cont.run&#x2F;lowering-async-await-in-rust&#x2F;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Understanding Rust Macros: Compilation &amp; Declarative Patterns</title>
        <published>2025-04-01T00:00:00+00:00</published>
        <updated>2025-04-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/understanding-rust-macros-compilation-declarative-patterns/"/>
        <id>https://kev1n8.github.io/posts/understanding-rust-macros-compilation-declarative-patterns/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/understanding-rust-macros-compilation-declarative-patterns/">&lt;h1 id=&quot;understanding-rust-macros-compilation-declarative-patterns&quot;&gt;Understanding Rust Macros: Compilation &amp;amp; Declarative Patterns&lt;&#x2F;h1&gt;
&lt;p&gt;Before deciding which type of macro to write, ask yourself the questions below:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Is this a basic find&#x2F;replace (e.g. implementing the same trait for different integer types or tuple lengths) or do I need some sort of conditional logic?&lt;&#x2F;li&gt;
&lt;li&gt;Do I need to track more &quot;state&quot; than a can be achieved with a simple pushdown accumulator?&lt;&#x2F;li&gt;
&lt;li&gt;Is this purely a syntactic operation or do I need to inspect a token&#x27;s value and do logic with it?&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;compiling-trip&quot;&gt;Compiling Trip&lt;&#x2F;h2&gt;
&lt;p&gt;Rust compilation goes through stages below:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Tokenization -- keywords&lt;&#x2F;li&gt;
&lt;li&gt;Parsing -- where AST constructed&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Macro Expansion&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Name Resolution and Type Checking&lt;&#x2F;li&gt;
&lt;li&gt;Intermediate Representation&lt;&#x2F;li&gt;
&lt;li&gt;Optimization&lt;&#x2F;li&gt;
&lt;li&gt;Code Generation -- to LLVM IR&lt;&#x2F;li&gt;
&lt;li&gt;LLVM Optimization&lt;&#x2F;li&gt;
&lt;li&gt;Machine Code Generation&lt;&#x2F;li&gt;
&lt;li&gt;Linking&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;strong&gt;Tokens tree is distinct from AST&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;macro-in-ast&quot;&gt;Macro in AST&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;# [ $arg ];&lt;&#x2F;code&gt; e.g. #[derive(Clone)], #[no_mangle], …&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;# ! [ $arg ];&lt;&#x2F;code&gt; e.g. #![allow(dead_code)], #![crate_name=&quot;blang&quot;], …&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;$name ! $arg;&lt;&#x2F;code&gt; e.g. println!(&quot;Hi!&quot;), concat!(&quot;a&quot;, &quot;b&quot;), …&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;$name ! $arg0 $arg1;&lt;&#x2F;code&gt; e.g. macro_rules! dummy { () =&amp;gt; {}; }.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;expansion&quot;&gt;Expansion&lt;&#x2F;h3&gt;
&lt;p&gt;Intuitively, this is done by &quot;expanding&quot; the AST on its &lt;em&gt;syntax extensions&lt;&#x2F;em&gt; invocations &lt;strong&gt;recursively&lt;&#x2F;strong&gt;, and level-up no more than 128(default).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;hygiene&quot;&gt;Hygiene&lt;&#x2F;h3&gt;
&lt;p&gt;Simply saying, in Rust, macro can&#x27;t &lt;code&gt;use&lt;&#x2F;code&gt; the identifier ouside or &lt;code&gt;create&lt;&#x2F;code&gt; an identifier then &lt;code&gt;use&lt;&#x2F;code&gt; outside of it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;declarative-macro&quot;&gt;Declarative Macro&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;macro_rules!&lt;&#x2F;code&gt; checks the rules one by one, perform expansion once the &lt;code&gt;matcher&lt;&#x2F;code&gt; matches.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;macro_rules!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    $&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;rule0&lt;&#x2F;span&gt;&lt;span&gt; ;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    $&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;rule1&lt;&#x2F;span&gt;&lt;span&gt; ;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; …&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    $&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;ruleN&lt;&#x2F;span&gt;&lt;span&gt; ;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;rule&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;matcher&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;expansion&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;macro_rules!&lt;&#x2F;code&gt;&#x27;s invocation does not expand in AST, the macro is registered internally in the compiler.&lt;&#x2F;p&gt;
&lt;p&gt;Repetition example&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ...&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sep rep&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;macro_rules! sample&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;ident&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        $&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; operation&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;sep&lt;&#x2F;code&gt; options: &lt;code&gt;,&lt;&#x2F;code&gt;, &lt;code&gt;;&lt;&#x2F;code&gt; or space by default
&lt;code&gt;rep&lt;&#x2F;code&gt; options: &lt;code&gt;*&lt;&#x2F;code&gt;, &lt;code&gt;+&lt;&#x2F;code&gt;, or &lt;code&gt;?&lt;&#x2F;code&gt; which can&#x27;t be used with &lt;code&gt;sep&lt;&#x2F;code&gt; since at most 1&lt;&#x2F;p&gt;
&lt;h3 id=&quot;minutiae&quot;&gt;Minutiae&lt;&#x2F;h3&gt;
&lt;p&gt;The following labels are that we can capture from the input:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;block -- capture a code block like &lt;code&gt;{...}&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;expr -- a lot&lt;&#x2F;li&gt;
&lt;li&gt;ident -- identifiers and keywords&lt;&#x2F;li&gt;
&lt;li&gt;item -- definitions like &lt;code&gt;struct Foo&lt;&#x2F;code&gt; or &lt;code&gt;pub use crate::mod&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;lifetime -- similar to ident but start with &lt;code&gt;&#x27;&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;literal -- immediate values&lt;&#x2F;li&gt;
&lt;li&gt;meta -- matches attributes&lt;&#x2F;li&gt;
&lt;li&gt;pat -- any kind of pattern&lt;&#x2F;li&gt;
&lt;li&gt;pat_param -- any pattern but no &lt;code&gt;|&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;path -- a path like &lt;code&gt;crate::mod&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;stmt -- a statement&lt;&#x2F;li&gt;
&lt;li&gt;tt -- a token tree&lt;&#x2F;li&gt;
&lt;li&gt;ty -- a type expression&lt;&#x2F;li&gt;
&lt;li&gt;vis -- visibility like &lt;code&gt;pub(crate)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;scoping&quot;&gt;Scoping&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;#[macro_use]&lt;&#x2F;code&gt;: &lt;code&gt;use&lt;&#x2F;code&gt; all the macro inside the first following mod, applicable for the whole &quot;level&quot;.
&lt;code&gt;#[macro_export]&lt;&#x2F;code&gt;: export the macro to outside (of &lt;code&gt;crate&lt;&#x2F;code&gt;), other users may &lt;code&gt;use mod::some_macro;&lt;&#x2F;code&gt;(only applicable for external crate).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;todo&quot;&gt;TODO&lt;&#x2F;h3&gt;
&lt;p&gt;There are more sections to learn. Maybe learn on practice.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;procedural-macro&quot;&gt;Procedural Macro&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;types&quot;&gt;Types&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;function-like&lt;&#x2F;em&gt; proc-macros -- &lt;code&gt;$name ! $arg&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;atribute&lt;&#x2F;em&gt; proc-macros -- &lt;code&gt;#[$arg]&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;derive&lt;&#x2F;em&gt; proc-macros -- &lt;code&gt;#[derive(...)]&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[proc_macro]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; my_proc_macro&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    TokenStream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[proc_macro_attribute]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; my_attribute&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; annotated_item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    TokenStream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[proc_macro_derive]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; my_derive&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;annotatated_item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TokenStream&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    TokenStream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;useful-third-party-tools&quot;&gt;Useful Third-party tools&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;proc-macro2&lt;&#x2F;li&gt;
&lt;li&gt;quote&lt;&#x2F;li&gt;
&lt;li&gt;syn&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Subtyping &amp; Variance in Rust Lifetime</title>
        <published>2024-12-16T00:00:00+00:00</published>
        <updated>2024-12-16T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/subtyping-variance-in-rust-lifetime/"/>
        <id>https://kev1n8.github.io/posts/subtyping-variance-in-rust-lifetime/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/subtyping-variance-in-rust-lifetime/">&lt;h1 id=&quot;rustsheng-ming-zhou-qi-subtyping-variance&quot;&gt;Rust生命周期 Subtyping &amp;amp; Variance&lt;&#x2F;h1&gt;
&lt;p&gt;Rust 使用生命周期来跟踪引用和所有权，而生命周期检查实现的背后是 &lt;strong&gt;subtyping&lt;&#x2F;strong&gt;（子类型） 和 &lt;strong&gt;variance&lt;&#x2F;strong&gt;（变型）。&lt;&#x2F;p&gt;
&lt;p&gt;Subtyping 和 variance 本是继承中使用的概念，但是 Rust 中并没有继承机制，Rust 中这两概念用在了生命周期检查上。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;subtyping&quot;&gt;Subtyping&lt;&#x2F;h2&gt;
&lt;p&gt;先看一个例子：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Note: debug expects two parameters with the *same* lifetime&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; b = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;b:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;static&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hello&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;world&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; world&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; world&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;world has a shorter lifetime than &amp;#39;static&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; world&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;一个&lt;strong&gt;保守&lt;&#x2F;strong&gt;的生命周期实现不会允许这样的行为，因为 &lt;code&gt;&#x27;static&lt;&#x2F;code&gt; 并不等于 &lt;code&gt;&#x27;world&lt;&#x2F;code&gt;。但是直觉告诉我们，这段代码这样做并没有安全问题。尝试编译这段代码，发现确实是可以正常通过检查的。这样灵活的约束得益于 &lt;strong&gt;subtyping&lt;&#x2F;strong&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;首先我们要知道：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;一个生命周期定义了代码的一个区域&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&#x27;long &amp;lt;: &#x27;short&lt;&#x2F;code&gt; 表示 &lt;code&gt;&#x27;long&lt;&#x2F;code&gt; 是 &lt;code&gt;&#x27;short&lt;&#x2F;code&gt; 的 subtype；当且仅当 &lt;code&gt;&#x27;long&lt;&#x2F;code&gt; 定义的代码区域完全包括了 &lt;code&gt;&#x27;short&lt;&#x2F;code&gt; 定义的区域&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;回到上面的例子，我们可以知道 &lt;code&gt;&#x27;static &amp;lt;: &#x27;world&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;同时，&lt;code&gt;debug(&amp;amp;&#x27;static str, &amp;amp;&#x27;world world)&lt;&#x2F;code&gt; 合法，因此我们说 &lt;code&gt;&amp;amp;&#x27;static str&lt;&#x2F;code&gt; 是 &lt;code&gt;&amp;amp;&#x27;a str&lt;&#x2F;code&gt; 的一个 subtype&lt;&#x2F;p&gt;
&lt;p&gt;可以认为在这里 &lt;code&gt;&amp;amp;&#x27;static str&lt;&#x2F;code&gt; 被“降级”为 &lt;code&gt;&amp;amp;&#x27;a str&lt;&#x2F;code&gt;，从而匹配了 &lt;code&gt;debug&lt;&#x2F;code&gt; 的函数签名。&lt;&#x2F;p&gt;
&lt;p&gt;也就是说，尽管生命周期不完全匹配，只要参数是函数签名要求的subtype，这个行为就是允许的。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;variance&quot;&gt;Variance&lt;&#x2F;h2&gt;
&lt;p&gt;Variance 描述了 subtype 属性在不同情况下是否可以保持下去的性质。&lt;&#x2F;p&gt;
&lt;p&gt;给定类型 &lt;code&gt;F&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;，其中 &lt;code&gt;T&lt;&#x2F;code&gt; 为 &lt;code&gt;Sub&lt;&#x2F;code&gt; or &lt;code&gt;Super&lt;&#x2F;code&gt;， variance 定义了三种关系：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;如果 &lt;code&gt;F&amp;lt;Sub&amp;gt;&lt;&#x2F;code&gt; 是 &lt;code&gt;F&amp;lt;Super&amp;gt;&lt;&#x2F;code&gt; 的 subtype，那么我们说 &lt;code&gt;F&lt;&#x2F;code&gt; 是 co-variant （协变）的；就好像 subtype 属性传递下去了&lt;&#x2F;li&gt;
&lt;li&gt;如果 &lt;code&gt;F&amp;lt;Super&amp;gt;&lt;&#x2F;code&gt; 是 &lt;code&gt;F&amp;lt;Sub&amp;gt;&lt;&#x2F;code&gt; 的 subtype，那么我们说 &lt;code&gt;F&lt;&#x2F;code&gt; 是 contra-variant （逆变）的；就好像 subtype 属性扭转了；这种情况在实际中非常少出现&lt;&#x2F;li&gt;
&lt;li&gt;其余情况，&lt;code&gt;F&lt;&#x2F;code&gt; 是 in-variant （不变）的；不能应用 subtype 属性&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;各类型和它们的 variances 如下表：&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;&#x27;a&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;T&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;U&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;amp;&#x27;a T &lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;amp;&#x27;a mut T&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;invariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;UnsafeCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;invariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Cell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;invariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;fn(T) -&amp;gt; U&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;contra&lt;&#x2F;strong&gt;variant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;*const T&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;covariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;*mut T&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;invariant&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;简单来说：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 和其他指针和集合同 &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Cell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 和其他具有内部可变性的同 &lt;code&gt;UnsafeCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;UnsafeCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 因为它的内部可变性因此同 &lt;code&gt;&amp;amp;mut T&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;*const T&lt;&#x2F;code&gt; 同 &lt;code&gt;&amp;amp;T&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;*mut T&lt;&#x2F;code&gt; 同 &lt;code&gt;&amp;amp;mut T&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;下面结合例子来理解这三种关系。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;covariance&quot;&gt;Covariance&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;a-t-over-a-is-covariant&quot;&gt;&lt;code&gt;&amp;amp;&#x27;a T&lt;&#x2F;code&gt; over &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; is covariant&lt;&#x2F;h3&gt;
&lt;p&gt;If &lt;code&gt;&#x27;a &amp;lt;: &#x27;b&lt;&#x2F;code&gt;, then &lt;code&gt;&amp;amp;&#x27;a T &amp;lt;: &amp;amp;&#x27;b T&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; b = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;b:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;debug&lt;&#x2F;code&gt; 的参数必须是相同类型 &lt;code&gt;T&lt;&#x2F;code&gt;，在这里类型推断为 &lt;code&gt;&amp;amp;&#x27;a str&lt;&#x2F;code&gt;，完整函数签名：&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fn debug&amp;lt;&#x27;a&amp;gt;(a: &amp;amp;&#x27;a str, b: &amp;amp;&#x27;a str)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;main&lt;&#x2F;code&gt; 中有 &lt;code&gt;a: &amp;amp;&#x27;static str&lt;&#x2F;code&gt; 和 &lt;code&gt;b: &amp;amp;&#x27;b str&lt;&#x2F;code&gt;，且 &lt;code&gt;&#x27;static &amp;lt;: &#x27;b&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;因为 &lt;code&gt;&amp;amp;&#x27;a T&lt;&#x2F;code&gt; 对 &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; 是 covariant 的，所以 &lt;code&gt;&amp;amp;&#x27;static str&lt;&#x2F;code&gt; 在这里可以“降级”为 &lt;code&gt;&amp;amp;&#x27;b str&lt;&#x2F;code&gt;，被 &lt;code&gt;debug&lt;&#x2F;code&gt; 接受。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-t-over-t-is-covariant&quot;&gt;&lt;code&gt;&amp;amp;&#x27;a T&lt;&#x2F;code&gt; over &lt;code&gt;T&lt;&#x2F;code&gt; is covariant&lt;&#x2F;h3&gt;
&lt;p&gt;If &lt;code&gt;t1 &amp;lt;: t2&lt;&#x2F;code&gt;, then &lt;code&gt;&amp;amp;&#x27;a t1 &amp;lt;: &amp;amp;&#x27;b t2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; b = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;b:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这次参数变成了 &lt;code&gt;(a: &amp;amp;&#x27;a T, b: T)&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;我们传入的参数为 &lt;code&gt;(&amp;amp;&#x27;b &amp;amp;&#x27;static str, &amp;amp;&#x27;b str)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;类型推导过程中，&lt;&#x2F;p&gt;
&lt;p&gt;当 &lt;code&gt;T = &amp;amp;&#x27;static str&#x27;&lt;&#x2F;code&gt;，编译器判定不能满足生命周期约束，因为 &lt;code&gt;&amp;amp;&#x27;b str &amp;lt;: &amp;amp;&#x27;static&lt;&#x2F;code&gt; 不成立；&lt;&#x2F;p&gt;
&lt;p&gt;当 &lt;code&gt;T = &amp;amp;&#x27;b str&lt;&#x2F;code&gt;，根据规则， &lt;code&gt;&#x27;static &amp;lt;: &#x27;b&lt;&#x2F;code&gt; =&amp;gt; &lt;code&gt;&amp;amp;&#x27;static str &amp;lt;: &amp;amp;&#x27;b str&lt;&#x2F;code&gt;。
从而可以将 &lt;code&gt;a: &amp;amp;&#x27;a &amp;amp;&#x27;static str&lt;&#x2F;code&gt; “降级”为 &lt;code&gt;&amp;amp;&#x27;a &amp;amp;&#x27;b str&lt;&#x2F;code&gt;，成功匹配了函数签名。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-mut-t-over-a-is-covariant&quot;&gt;&lt;code&gt;&amp;amp;&#x27;a mut T&lt;&#x2F;code&gt; over &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; is covariant&lt;&#x2F;h3&gt;
&lt;p&gt;If &lt;code&gt;&#x27;a &amp;lt;: &#x27;b&lt;&#x2F;code&gt;, then &lt;code&gt;&amp;amp;&#x27;a mut T &amp;lt;: &amp;amp;&#x27;b mut T&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;参数：&lt;code&gt;(&amp;amp;&#x27;static mut &amp;amp;&#x27;static str)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;根据规则： &lt;code&gt;&#x27;static &amp;lt;: &#x27;a&lt;&#x2F;code&gt; =&amp;gt; &lt;code&gt;&amp;amp;&#x27;static mut &amp;amp;&#x27;static str &amp;lt;: &amp;amp;&#x27;a mut &amp;amp;&#x27;static str&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;类型推断 &lt;code&gt;T = &amp;amp;&#x27;static str&lt;&#x2F;code&gt;，匹配成功。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;invariance&quot;&gt;Invariance&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;a-mut-t-over-t-is-invariant&quot;&gt;&lt;code&gt;&amp;amp;&#x27;a mut T&#x27;&lt;&#x2F;code&gt; over &lt;code&gt;T&lt;&#x2F;code&gt; is invariant&lt;&#x2F;h3&gt;
&lt;p&gt;Even if &lt;code&gt;t1 &amp;lt;: t2&lt;&#x2F;code&gt;, there&#x27;s no &lt;code&gt;&amp;amp;&#x27;a mut t1 &amp;lt;: &amp;amp;&#x27;a mut t2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, b=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; won&amp;#39;t compile&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;参数：&lt;code&gt;(&amp;amp;&#x27;b mut &amp;amp;&#x27;static str, &amp;amp;&#x27;b &#x27;&amp;amp;&#x27;b str)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;尽管有 &lt;code&gt;&amp;amp;&#x27;static str &amp;lt;: &amp;amp;&#x27;b str&lt;&#x2F;code&gt;（根据 &lt;code&gt;&amp;amp;&#x27;a T&lt;&#x2F;code&gt; over &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; is covariant）&lt;&#x2F;p&gt;
&lt;p&gt;我们不能得出 &lt;code&gt;&amp;amp;&#x27;b mut &amp;amp;&#x27;static str &amp;lt;: &amp;amp;&#x27;b mut &amp;amp;&#x27;b str&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;因此 &lt;code&gt;T&lt;&#x2F;code&gt; 必须是 &lt;code&gt;&amp;amp;&#x27;static str&lt;&#x2F;code&gt; （即不允许“降级”）&lt;&#x2F;p&gt;
&lt;p&gt;而 &lt;code&gt;&amp;amp;&#x27;b str &amp;lt;: &amp;amp;&#x27;static str&lt;&#x2F;code&gt; 不成立，所以两个参数的 &lt;code&gt;T&lt;&#x2F;code&gt; 无法统一，编译无法通过。&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;下面的代码是可以正常运行的&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, b=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug_str&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;amp;str, with an implicit lifetime.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    debug_str&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;参数： &lt;code&gt;(&amp;amp;&#x27;b mut &amp;amp;&#x27;b str, &amp;amp;&#x27;b str)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;这是因为在没有手动指定生命周期的情况下，编译器会根据代码逻辑自动推断生命周期。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;a&lt;&#x2F;code&gt; 的生命周期被“压缩”到了 &lt;code&gt;&#x27;b&lt;&#x2F;code&gt;，使得它能匹配 &lt;code&gt;debug&lt;&#x2F;code&gt; 签名同时不违反 invariance。&lt;&#x2F;p&gt;
&lt;p&gt;而下面的代码：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, b=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; debug_static&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static T&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;amp;str, with no explicit lifetime.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    debug_static&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; 1.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; string&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        debug&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; 2.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; 1. sets lifetime of `a` to &amp;#39;static&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; 2. needs lifetime of `a` at least longer than &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;上面的代码是不能编译通过的，因为编译器推断出的生命周期&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;&#x27;static&lt;&#x2F;code&gt; 以及&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;at least longer than &#x27;b&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;code&gt;1.&lt;&#x2F;code&gt; 和 &lt;code&gt;2.&lt;&#x2F;code&gt; 任意存在都会导致违反 &lt;code&gt;&amp;amp;&#x27;a mut T&lt;&#x2F;code&gt; 对 &lt;code&gt;T&lt;&#x2F;code&gt; 的 invariance.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;contravariance&quot;&gt;Contravariance&lt;&#x2F;h2&gt;
&lt;p&gt;最后，也是是唯一一个的 contra-variant。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fn(T) -&amp;gt; U&lt;&#x2F;code&gt; over &lt;code&gt;T&lt;&#x2F;code&gt; is contravariant.&lt;&#x2F;p&gt;
&lt;p&gt;If &lt;code&gt;t1 &amp;lt;: t2&lt;&#x2F;code&gt;, then &lt;code&gt;fn(t2) &amp;lt;: fn(t1)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;直接看例子：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;thread_local!&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; static&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; StaticVecs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RefCell&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RefCell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; saves the input given into a thread local `Vec&amp;lt;&amp;amp;&amp;#39;static str&amp;gt;`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;static str&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    StaticVecs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_borrow_mut&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; Calls the function with it&amp;#39;s input (must have the same lifetime!)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; demo&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: fn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    f&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    demo&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; store&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; &amp;quot;hello&amp;quot; is &amp;#39;static. Can call `store` fine&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; smuggle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;smuggle&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; `&amp;amp;smuggle` is not static. If we were to call `store` with `&amp;amp;smuggle`,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; we would have pushed an invalid lifetime into the `StaticVecs`.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Therefore, `fn(&amp;amp;&amp;#39;static str)` cannot be a subtype of `fn(&amp;amp;&amp;#39;a str)`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        demo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;smuggle&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; store&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; use after free...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    StaticVecs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_borrow&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;v:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;References&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;subtyping.html
https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Covariance_and_contravariance_(computer_science)&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Extending DataFusion: Custom Query Plans &amp; Optimizer Rules</title>
        <published>2024-09-21T00:00:00+00:00</published>
        <updated>2024-09-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/extending-datafusion-custom-query-plans-optimizer-rules/"/>
        <id>https://kev1n8.github.io/posts/extending-datafusion-custom-query-plans-optimizer-rules/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/extending-datafusion-custom-query-plans-optimizer-rules/">&lt;h2 id=&quot;kuo-zhan-datafusionzhi-zi-ding-yi-ji-hua&quot;&gt;扩展Datafusion之自定义计划&lt;&#x2F;h2&gt;
&lt;p&gt;上一篇扩展datafusion介绍了通过自定义的 &lt;code&gt;TableProvider&lt;&#x2F;code&gt;, &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt; 和 &lt;code&gt;DataSink&lt;&#x2F;code&gt; 来帮助我们达到下面的目的：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;使用上下文注册 &lt;code&gt;TableProvider&lt;&#x2F;code&gt; 定向 &lt;code&gt;scan&lt;&#x2F;code&gt; 和 &lt;code&gt;Insert_into&lt;&#x2F;code&gt; 执行时的调用的对象（&lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;）&lt;&#x2F;li&gt;
&lt;li&gt;自定义的 &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt; 执行从rocksdb读取表数据的操作&lt;&#x2F;li&gt;
&lt;li&gt;自定义的 &lt;code&gt;DataSink&lt;&#x2F;code&gt;，提供 &lt;code&gt;write_all&lt;&#x2F;code&gt; 函数供datafusion的 &lt;code&gt;DataSinkExec&lt;&#x2F;code&gt; 调用，后者在 &lt;code&gt;insert_into&lt;&#x2F;code&gt; 的时候被创建&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;而本文也将从几个要自定义实现的 &lt;code&gt;trait&lt;&#x2F;code&gt; 开始介绍，分别是：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;QueryPlanner&lt;&#x2F;code&gt; -- 专门用于调用各 &lt;code&gt;Plannner&lt;&#x2F;code&gt; 生成 &lt;code&gt;LogicalPlan&lt;&#x2F;code&gt; 和 &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;，本文用于注册自定义的 &lt;code&gt;ExtensionPlanner&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;OptimizerRule&lt;&#x2F;code&gt; -- 用于自定义一个优化规则，本文实现一个规则将datafusion自带的 &lt;code&gt;DML::DELETE&lt;&#x2F;code&gt; &lt;code&gt;PlanNode&lt;&#x2F;code&gt; 替换为自定义的 &lt;code&gt;DeletePlanNode&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;UserDefinedLogicalNodeCore&lt;&#x2F;code&gt; -- 用于实现自定 &lt;code&gt;LogicPlanNode&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ExtensionPlanner&lt;&#x2F;code&gt; -- 用于根据输入 &lt;code&gt;LogicPlanNode&lt;&#x2F;code&gt; 创建一个 &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ExecuionPlan&lt;&#x2F;code&gt; -- 类似上一篇，只不过本文将实现 &lt;code&gt;Delete&lt;&#x2F;code&gt; 逻辑&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;queryplanner&quot;&gt;QueryPlanner&lt;&#x2F;h2&gt;
&lt;p&gt;这是 &lt;code&gt;SessionState&lt;&#x2F;code&gt; 的一个成员，我们可以看看如果不为其添加任何扩展，它会如何初始化：&lt;&#x2F;p&gt;
&lt;h3 id=&quot;chu-shi-hua-lu-jing-yuan-ma-yi-lan&quot;&gt;初始化路径源码一览&lt;&#x2F;h3&gt;
&lt;p&gt;首先是出处，&lt;code&gt;query_planner&lt;&#x2F;code&gt; 作为成员，需要满足 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt; 动态分发：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionState&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;	&#x2F;&#x2F;&#x2F; Responsible for planning `LogicalPlan`s, and `ExecutionPlan`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    query_planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; QueryPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Send&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Sync&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;其次，我们看 &lt;code&gt;SessionStateBuilder&lt;&#x2F;code&gt;，这里省略了其它属性设置 ：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionStateBuilder&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;	pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; build&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionState&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            query_planner&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionState&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; 如果没有设定好的，则使用DefaultQueryPlanner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            query_planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; query_planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap_or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DefaultQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {})),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;			...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;没有规定一定要通过Builder来设置 &lt;code&gt;SessionState&lt;&#x2F;code&gt;，重要的是我们知道了Datafusion提供了默认的 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;接着，来看这个 &lt;code&gt;DefaultQueryPlanner&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; QueryPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; Given a `LogicalPlan`, create an [`ExecutionPlan`] suitable for execution&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        logical_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        session_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SessionState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; datafusion_common&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultPhysicalPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        planner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_state&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .await&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;可见，datafusion又把初始化交给了 &lt;code&gt;DefaultPhysicalPlanner&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Default&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultPhysicalPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; 如果我们注册了扩展，则会保存在这里&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    extension_planners&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExtensionPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Send&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Sync&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; PhysicalPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultPhysicalPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; Create a physical plan from a logical plan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        logical_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        session_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SessionState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; 具体的创建plan流程&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        match self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;handle_explain&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_state&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await?&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            None&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;create_initial_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_state&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;optimize_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_state&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {})&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;到这里我们就知道，&lt;code&gt;QueryPlanner&lt;&#x2F;code&gt; 起到一个根据 &lt;code&gt;LogicalPlan&lt;&#x2F;code&gt; 创建 &lt;code&gt;PhysicalPlan&lt;&#x2F;code&gt; (&lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;) 的作用，具体的计划创建过程（关于从 &lt;code&gt;LogicalPlanNode&lt;&#x2F;code&gt; 到 &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt; 可以参考后续 &lt;code&gt;Planner&lt;&#x2F;code&gt; 的定义）就不展开了。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;ru-he-zi-ding-yi&quot;&gt;如何自定义&lt;&#x2F;h3&gt;
&lt;p&gt;至于自定义的 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt;， 就是简单添加一个自定义的 &lt;code&gt;Planner&lt;&#x2F;code&gt; 到 &lt;code&gt;DefaultPhysicalPlanner&lt;&#x2F;code&gt; 中去：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeleteQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    table_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; QueryPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeleteQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        logical_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        session_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SessionState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; 这里并非使用默认，而是添加了一个自定义的DeletePlanner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultPhysicalPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_extension_planners&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            DeletePlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                table_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: self.&lt;&#x2F;span&gt;&lt;span&gt;table_id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;db),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        planner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_state&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .await&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这样一来，后续的sql操作在转换计划的时候，注册了这个 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt; 的 &lt;code&gt;SessionState&lt;&#x2F;code&gt; 就知道 &lt;code&gt;DeletePlanner&lt;&#x2F;code&gt;，那么在遇到 &lt;code&gt;DeleteLogicalPlanNode&lt;&#x2F;code&gt; 的时候也就知道如何生成相应的 &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;，也就支持了 &lt;code&gt;Delete&lt;&#x2F;code&gt; 操作。&lt;&#x2F;p&gt;
&lt;p&gt;那么问题来了，怎么把Datafusion中原有的 &lt;code&gt;DML::Delete&lt;&#x2F;code&gt; 这个 &lt;code&gt;PlanNode&lt;&#x2F;code&gt; 替换成真正的，自定义的 &lt;code&gt;PlanNode&lt;&#x2F;code&gt; 呢？&lt;&#x2F;p&gt;
&lt;h2 id=&quot;optimizerrule&quot;&gt;OptimizerRule&lt;&#x2F;h2&gt;
&lt;p&gt;顾名思义，这个 &lt;code&gt;trait&lt;&#x2F;code&gt; 可以让我们自定义一个逻辑优化器规则。本文要做的，就是将datafusion解析 &lt;code&gt;DELETE&lt;&#x2F;code&gt; 操作产生的 &lt;code&gt;LogicPlan::DML(DELETE)&lt;&#x2F;code&gt;  给换成我们自己的实现，前者在执行阶段会返回错误：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DefaultPhysicalPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; map_logical_node_to_physical&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        session_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SessionState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        children&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ChildrenContainer&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exec_node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        	LogicalPlan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Extension&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Extension&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span&gt; })&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; maybe_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; children&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; children&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in &amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;extension_planners {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; maybe_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;is_some&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; logical_input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;inputs&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    maybe_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; planner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;plan_extension&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                            node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_input&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                            &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;children&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                            session_state&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; maybe_plan&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                        Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                        _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; plan_err!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;No installed planner was able to convert the custom node to an execution plan: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; Ensure the ExecutionPlan&amp;#39;s schema matches the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; declared logical schema to catch and warn about&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; logic errors when creating user defined plans.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                if !&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;matches_arrow_schema&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; plan_err!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Extension planner for &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; created an ExecutionPlan with mismatched schema. \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;                            LogicalPlan schema: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, ExecutionPlan schema: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                            node&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    plan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;			LogicalPlan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Dml&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;dml&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; DataFusion is a read-only query engine, but also a library, so consumers may implement this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; not_impl_err!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Unsupported logical plan: Dml(&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; dml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;op);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;从上面的代码可以看见，如果我们没有用我们自己的实现（也就是 &lt;code&gt;LogicalPlan::Extension&lt;&#x2F;code&gt;）替换 &lt;code&gt;LogicalPlan::Dml(dml)&lt;&#x2F;code&gt;，就会返回错误。而如果通过我们自定义的rule将这个计划结点换成，就可以执行我们自己的实现（对应 &lt;code&gt;DeleteLogicalPlanNode&lt;&#x2F;code&gt;）。&lt;&#x2F;p&gt;
&lt;p&gt;实现 &lt;code&gt;OptimizerRule&lt;&#x2F;code&gt; 关键是实现 &lt;code&gt;re_write&lt;&#x2F;code&gt; 方法，这个方法检查当前结点是否为目标结点（&lt;code&gt;LogicalPlan::Dml(delete)&lt;&#x2F;code&gt;），如果是的话就用自定义结点取代（&lt;code&gt;DeleteLogicalPlanNode&lt;&#x2F;code&gt;），代码如下：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeleteReplaceRule&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; OptimizerRule&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeleteReplaceRule&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; rewrite&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; OptimizerConfig&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Transformed&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DataFusionError&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LogicalPlan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Dml&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DmlStatement&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            table_name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            op&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; WriteOp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Delete&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            input&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;         &#x2F;&#x2F; table source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            output_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; single count&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        })&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; plan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; input should have only 1 item, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; it can only be `LogicalPlan::Filter`, or&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; `LogicalPlan::Scan`?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _inputt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Transformed&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Extension&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Extension&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DeletePlanNode&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; output_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;expressions&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            })))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Transformed&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;no&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;userdefinedlogicalnodecore&quot;&gt;UserDefinedLogicalNodeCore&lt;&#x2F;h2&gt;
&lt;p&gt;直接贴代码：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeletePlanNode&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DFSchemaRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; UserDefinedLogicalNodeCore&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeletePlanNode&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; with_exprs_and_inputs&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        exprs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;input size inconsistent&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;swap_remove&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; 假定只有一个子节点&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;schema),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exprs&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;实际运行时，这个函数会被调用很多次，但都没有任何改变。To be found...&lt;&#x2F;p&gt;
&lt;h2 id=&quot;extensionplanner&quot;&gt;ExtensionPlanner&lt;&#x2F;h2&gt;
&lt;p&gt;重头戏之一，我们在上面已经实现了 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt;, &lt;code&gt;OptimizerRule&lt;&#x2F;code&gt;, &lt;code&gt;PlanNode&lt;&#x2F;code&gt;，这三者结合已经能够做到下面的事情：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;为 &lt;code&gt;SessionState&lt;&#x2F;code&gt; 注册 &lt;code&gt;QueryPlanner&lt;&#x2F;code&gt; 和 &lt;code&gt;OptimizerRule&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;sql执行DML之 &lt;code&gt;DELETE&lt;&#x2F;code&gt; 操作的时候，生成的逻辑计划将是自定义的逻辑计划&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;但是，&lt;code&gt;DefaultPhysicalPlanner&lt;&#x2F;code&gt; 并不知道我们自定义的逻辑计划要生成什么样的执行计划。所以，&lt;code&gt;ExtensionPlanner&lt;&#x2F;code&gt; 就要站出来发挥作用了。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeletePlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    table_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExtensionPlanner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DeletePlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; plan_extension&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _planner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; PhysicalPlanner&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; UserDefinedLogicalNode&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        logical_inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;LogicalPlan&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        physical_inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _session_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SessionState&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;delete_node&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_any&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;downcast_ref&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DeletePlanNode&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;logical_inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;physical_inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; output_schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;delete_node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;schema)));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; input_schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;physical_inputs&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;DeleteExec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    self.&lt;&#x2F;span&gt;&lt;span&gt;table_id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    &amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;db,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;input_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;output_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;physical_inputs&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                )))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;node&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;逻辑就是：查看当前逻辑结点是否为“我”负责的，如果是的话则生成对应执行计划，否则返回None，datafusion会自动寻找其它对应的 &lt;code&gt;Planner&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;executionplan&quot;&gt;ExecutionPlan&lt;&#x2F;h2&gt;
&lt;p&gt;终于回到我们的 &lt;code&gt;KVTable&lt;&#x2F;code&gt; 了，我们在这里实现真正的删除操作，目的是实现从表中（db中）删除对应的行，而这些行会通过 &lt;code&gt;input&lt;&#x2F;code&gt; 过滤进来。每个行有一个唯一标识，即 &lt;code&gt;row_id&lt;&#x2F;code&gt;，删除操作会遍历表的每个列，然后遍历每个要删除的 &lt;code&gt;row_id&lt;&#x2F;code&gt;，这样可以构造出目标 &lt;code&gt;key&lt;&#x2F;code&gt;， &lt;code&gt;t[tid]_c[name]_r[rid]&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;实现如下：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; This method takes in the batch that is supposed to be deleted, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; delete them from the table of self.table_id, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; return rows deleted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;	fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; delete_batch_from_table&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RecordBatch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; by design, the `row_id` is the last column&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Iter over the `target` array to get each `row_id` to delete&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; as_string_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in self.&lt;&#x2F;span&gt;&lt;span&gt;in_schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;eq&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;row_id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                continue&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; break&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; target&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; id&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                    None&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                        todo!&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                    Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                        cnt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                        &#x2F;&#x2F; TODO: Change type of `row_id` from Utf8 to u64&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; make_row_key&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;table_id,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        self.&lt;&#x2F;span&gt;&lt;span&gt;db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;delete&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cnt&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;至此，我们的 &lt;code&gt;KVTable&lt;&#x2F;code&gt; 就支持删除操作了。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;bonus-ce-shi&quot;&gt;BONUS：测试&lt;&#x2F;h2&gt;
&lt;p&gt;同样的，贴一份基础测试，验证删除的有效性：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	#[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; basic_delete_test&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;open_default&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;.&#x2F;tmp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Registering the custom planners and rule&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionConfig&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_target_partitions&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; runtime&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;RuntimeEnv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionStateBuilder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_config&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;config&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_runtime_env&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;runtime&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_default_features&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_query_planner&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DeleteQueryPlanner&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                table_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1003&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;with_optimizer_rule&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DeleteReplaceRule&lt;&#x2F;span&gt;&lt;span&gt; {}))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionContext&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new_with_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;state&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;首先，我们创建一个 &lt;code&gt;SessionState&lt;&#x2F;code&gt; ，并注册自定义的 &lt;code&gt;Rule&lt;&#x2F;code&gt; 和 &lt;code&gt;Planner&lt;&#x2F;code&gt;，并根据这个State创建Datafusion的上下文。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create table for testing and register&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;c1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;row_id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ])));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;KVTableMeta&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1003&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            highest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; default, unknown&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_provider&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;KVTable&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;table_meta&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;)));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Since create table is not used here, we have to init the meta of table in db first&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta_key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;make_key&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta_val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;make_value&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;put&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;meta_key&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta_val&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;register_table&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_provider&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;接下来，创建表，并注册 &lt;code&gt;TableProvider&lt;&#x2F;code&gt; 到上下文。因为还没有实现 &lt;code&gt;CREATE&lt;&#x2F;code&gt; DDL，所以这里手动往 &lt;code&gt;db&lt;&#x2F;code&gt; 中添加表的元数据。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;		&#x2F;&#x2F; Now insert table with init data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; inserted&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ctx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;INSERT INTO t VALUES(&amp;#39;hello&amp;#39;, &amp;#39;001&amp;#39;), (&amp;#39;world&amp;#39;, &amp;#39;002&amp;#39;), (&amp;#39;!&amp;#39;, &amp;#39;003&amp;#39;)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .await&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Should have inserted 3 row&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; inserted&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; as_uint64_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;()[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Now try to delete from the table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; deleted&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;DELETE FROM t where c1=&amp;#39;!&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; deleted&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;logical_plan&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; deleted&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; as_uint64_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;()[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Should have deleted 1 row&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;创建完表后，利用之前实现的 &lt;code&gt;insert_into&lt;&#x2F;code&gt; 插入一些数据（这里有一个隐藏的问题，因为 &lt;code&gt;row_id&lt;&#x2F;code&gt; 是显式地在表schema中的，插入时其实并不知道具体的 &lt;code&gt;row_id&lt;&#x2F;code&gt;，目前的方法是随意输入值填充，但是后期的话应该要考虑让这个列对用户来说是透明的），插入后尝试删除行。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;		&#x2F;&#x2F; Now scan the table and compare&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; expected&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RecordBatch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;try_new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;StringArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;StringArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;000001&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;000002&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; df&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;SELECT c1 FROM t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; df&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; res&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; pretty_format_batches&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_slice&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; res&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; out&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; bind&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;out&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;expected&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;删除成功后，执行 &lt;code&gt;scan&lt;&#x2F;code&gt; 操作，查看输出是否和预期相同。&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Extending DataFusion: Connecting an External KV Store</title>
        <published>2024-09-13T00:00:00+00:00</published>
        <updated>2024-09-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/extending-datafusion-connecting-an-external-kv-store/"/>
        <id>https://kev1n8.github.io/posts/extending-datafusion-connecting-an-external-kv-store/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/extending-datafusion-connecting-an-external-kv-store/">&lt;h2 id=&quot;kuo-zhan-datafusionzhi-cong-wai-bu-kvcun-chu&quot;&gt;扩展Datafusion之从外部KV存储&lt;&#x2F;h2&gt;
&lt;p&gt;作为library，Datafusion在各个层次都提供了Extension trait，实现这些trait可以扩展datafusion的功能，非常方便。本文主要介绍Datafusion提供的&lt;code&gt;TableProvider&lt;&#x2F;code&gt;, &lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt; 和 &lt;code&gt;DataSink&lt;&#x2F;code&gt;三个&lt;code&gt;trait&lt;&#x2F;code&gt;。通过这几个&lt;code&gt;trait&lt;&#x2F;code&gt;我们可以实现让只能在内存(&lt;code&gt;datafusion::datasource::MemTable&lt;&#x2F;code&gt;)中的数据存储到任何我们想要的位置。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tableprovider&quot;&gt;TableProvider&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;TableProvider&lt;&#x2F;code&gt;顾名思义，让一个结构具有提供表相关功能的能力，一系列方法中最核心的就是要实现&lt;code&gt;scan&lt;&#x2F;code&gt;方法（当然，还有&lt;code&gt;insert_into&lt;&#x2F;code&gt;方法）。&lt;&#x2F;p&gt;
&lt;p&gt;一般来说，要让自定义的数据源来&lt;code&gt;TableProvider&lt;&#x2F;code&gt;。在我的实现中，我把KV存储作为&lt;code&gt;TableProvider&lt;&#x2F;code&gt;，使用的是RocksDB，那么相关的结构体就是这样：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTable&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTableMetaRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;我的实现比较简陋，只有一些必要的成员。接下来为了实现扫描功能，这个&lt;code&gt;TableProvider&lt;&#x2F;code&gt;需要有一个&lt;code&gt;scan&lt;&#x2F;code&gt;函数（和&lt;code&gt;insert_into&lt;&#x2F;code&gt;函数，如果想要支持写入到KV的话），返回&lt;code&gt;SendableStream&lt;&#x2F;code&gt;供后续异步回调数据。&lt;&#x2F;p&gt;
&lt;p&gt;但是实际在做的时候呢，并不会直接把执行细节放在数据源这里，而是通过构造一个&lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;来执行。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TableProvider&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVSource&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; scan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Session&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        projection&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _filters&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _limit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;create_scan_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;table_id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            projection&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;executionplan&quot;&gt;ExecutionPlan&lt;&#x2F;h2&gt;
&lt;p&gt;一个&lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;的职责，就是提供一个&lt;code&gt;execute&lt;&#x2F;code&gt;方法，能够返回&lt;code&gt;SendableStream&lt;&#x2F;code&gt;。而这个&lt;code&gt;Stream&lt;&#x2F;code&gt;会回调&lt;code&gt;ExecutionPlan&lt;&#x2F;code&gt;内部的一个方法，这个被回调的方法就是真正的功能实现的地方。以&lt;code&gt;DBTableScanExec&lt;&#x2F;code&gt;为例：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DBTableScanExec&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SchemaRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    cache&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; PlanProperties&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;我的初步实现按照以下的想法和限制：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;数据类型只支持String，便于统一&lt;code&gt;put&lt;&#x2F;code&gt;和&lt;code&gt;get&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;key的编码方式是 &lt;code&gt;t[table_id]_c[col_name]_r[row_id]&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;暂不支持&lt;code&gt;null&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;那么就可以有下面的&lt;code&gt;read_columns&lt;&#x2F;code&gt;方法：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; read_columns&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;ArrayRef&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;ArrayRef&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in &amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fields {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; Temporary ugly code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; start_key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;_c&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;_r000001&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;id,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; prefix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;_c&lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;id,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iterator&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;IteratorMode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;From&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;start_key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_bytes&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; rocksdb&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Direction&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Forward&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; values&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            while&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span&gt;)))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; Safety:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; All keys and values are supposed to be encoded from utf8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                unsafe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; key_str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from_utf8_unchecked&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_vec&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    if !&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key_str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;starts_with&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;prefix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value_str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from_utf8_unchecked&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_vec&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    values&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;value_str&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; array&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ArrayRef&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;StringArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;values&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;array&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        result&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这样一来，就可以查本&lt;code&gt;DBTableScanExec&lt;&#x2F;code&gt;实例对应&lt;code&gt;table_id&lt;&#x2F;code&gt;和&lt;code&gt;schema&lt;&#x2F;code&gt;的所有数据了。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;datasink&quot;&gt;DataSink&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;DataSink&lt;&#x2F;code&gt;的特点是有一个&lt;code&gt;write_all&lt;&#x2F;code&gt;方法，它的参数提供了一个&lt;code&gt;Stream&lt;&#x2F;code&gt;表示数据源，我们要实现的则是将&lt;code&gt;Stream&lt;&#x2F;code&gt;带来的数据写入&quot;Sink&quot;中，可以是各种目标，我这里的实现是将数据放进KV存储引擎中。&lt;&#x2F;p&gt;
&lt;p&gt;首先说明，实现&lt;code&gt;CustomDataSink&lt;&#x2F;code&gt;的目的是让Datafusion的&lt;code&gt;DataSinkExec&lt;&#x2F;code&gt;能够执行自定义的&lt;code&gt;write_all&lt;&#x2F;code&gt;方法。这个流程，举例来说，先在&lt;code&gt;ctx&lt;&#x2F;code&gt;中注册了已经实现了&lt;code&gt;KVTable&lt;&#x2F;code&gt;，然后使用&lt;code&gt;LogicPlanBuilder&lt;&#x2F;code&gt;构建一个简单的&lt;code&gt;insert_into&lt;&#x2F;code&gt; 逻辑计划。这时候&lt;code&gt;KVTable&lt;&#x2F;code&gt;中实现的&lt;code&gt;insert_into&lt;&#x2F;code&gt;就派上用场了，它会返回一个&lt;code&gt;DataSinkExec&lt;&#x2F;code&gt;附带了目标Sink的信息（也就是我们实现的&lt;code&gt;KVSink&lt;&#x2F;code&gt;）。再用&lt;code&gt;ctx&lt;&#x2F;code&gt;生成执行计划，最后调用&lt;code&gt;collect&lt;&#x2F;code&gt;回调计算函数，此时&lt;code&gt;write_all&lt;&#x2F;code&gt;就会在数据准备好之后被调用。&lt;&#x2F;p&gt;
&lt;p&gt;下面来看关键代码：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[async_trait]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; TableProvider&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVSource&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; insert_into&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Session&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _overwrite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; bool&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sink&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;KVTableSink&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;table_id,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;db));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;DataSinkExec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            input&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; input source informed by the caller of insert_into&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            sink&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;   &#x2F;&#x2F; target sink&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            None&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这个函数帮助我们构造执行计划，&lt;code&gt;DataSinkExec&lt;&#x2F;code&gt;会在执行的时候调用&lt;code&gt;write_all&lt;&#x2F;code&gt;，参考下面的&lt;code&gt;DataSinkExec::execute&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ExecutionPlan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DataSinkExec&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;	fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; execute&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        partition&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        context&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;TaskContext&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;SendableRecordBatchStream&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; partition&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; internal_err!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;DataSinkExec can only be called on partition 0!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; execute_input_stream&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; Fetch input stream here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;input),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;sink_schema),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;            0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; count_schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;count_schema);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sink&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self.&lt;&#x2F;span&gt;&lt;span&gt;sink);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; futures&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;once&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;async move&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            sink&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;make_count_batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; write_all here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;boxed&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;RecordBatchStreamAdapter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            count_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            stream&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;我的实现没有考虑context要求和多个partition的情况，只实现了基本的写入。其中&lt;code&gt;put_batch_into_db&lt;&#x2F;code&gt;的实现细节就不展开了，大致就是将一个&lt;code&gt;cell&lt;&#x2F;code&gt;（不是很高效的value单位）对应一个&lt;code&gt;key&lt;&#x2F;code&gt;（编码方式大概是：&lt;code&gt;t[table_id]_c[col_id]_r[row_id]&lt;&#x2F;code&gt;）存入rocksdb。下面是我的&lt;code&gt;write_all&lt;&#x2F;code&gt;实现：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;	async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; write_all&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SendableRecordBatchStream&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        _context&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;TaskContext&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; data&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;transpose&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            cnt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;put_batch_into_db&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;cnt&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;bonus-ce-shi&quot;&gt;BONUS：测试&lt;&#x2F;h2&gt;
&lt;p&gt;另外补充一下写的几个基本测试，主要是记录一下测试的方法。&lt;&#x2F;p&gt;
&lt;p&gt;首先，是元数据的编解码测试：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; test_meta_encode_decode&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTableMeta&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1002&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;TableTest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;column1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;column2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]))),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            highest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;make_key&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;make_value&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;mt1002&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into_bytes&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t1002_TableTest_0_c2_column1_column2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into_bytes&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; decode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTableMeta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;id,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; decode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;id);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;highest,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; decode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;highest);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; decode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;name);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;对于&lt;code&gt;DBTableScanExec&lt;&#x2F;code&gt;结构，定义表元数据、直接通过KV的API添加100个元素，然后使用&lt;code&gt;DBTableScanExec&lt;&#x2F;code&gt;执行返回的&lt;code&gt;Stream&lt;&#x2F;code&gt;获取&lt;code&gt;Batch&lt;&#x2F;code&gt;后逐行比对：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; test_read_from_db&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;KVTableMeta&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 101&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ])),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            highest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; key_prefix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;t101_chello_r&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hell&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ]));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; DB&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;open_default&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;.&#x2F;tmp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTable&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;meta&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;key_prefix&lt;&#x2F;span&gt;&lt;span&gt;}{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;i:0&amp;gt;6&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into_bytes&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;put&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into_bytes&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DBTableScanExec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; table_exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;execute&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;TaskContext&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;()));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; result&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;out&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; out&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                return&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; output&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Only 1 batch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                    assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;num_rows&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; row&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; as_string_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;col&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;enumerate&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;                        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                        &#x2F;&#x2F; println!(&amp;quot;{i} {}&amp;quot;, row.unwrap());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;最后，对&lt;code&gt;KVTableSink&lt;&#x2F;code&gt;进行测试，目的是为了确保数据确实有通过&lt;code&gt;insert_into&lt;&#x2F;code&gt;插入表中：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; Create a `KVTable` with a single column and `insert into` it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;&#x2F; by `values`, check if the data is inserted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; test_db_write&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;()&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create a new schema with one field called &amp;quot;a&amp;quot; of type Int32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;)]));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create a new batch of data to insert into the table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RecordBatch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;try_new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;StringArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]))],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Run the experiment and obtain the resulting data in the table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; resulting_data_in_table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            experiment&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;()]],&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;()]])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Ensure that the table now contains two batches of data in the same partition&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; resulting_data_in_table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; arr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; as_string_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;col&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                arr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;StringArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; todo: remove test table after this test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;	&#x2F;&#x2F;&#x2F; This function create a table with `initial_data` to &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;	&#x2F;&#x2F;&#x2F; insert `inserted_data` into and return the final batch of the table.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    async fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; experiment&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SchemaRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        initial_data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RecordBatch&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        inserted_data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RecordBatch&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RecordBatch&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; expected_count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; inserted_data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;flat_map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batches&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batches&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;num_rows&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create a new session context&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SessionContext&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create meta of a table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; dest_meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;KVTableMeta&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1002&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Dest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]))),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            highest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create KV store&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; DB&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;open_default&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;tmp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; db&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create and register the initial table with the provided schema and data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; initial_table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;KVTable&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;try_new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;dest_meta&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; initial_data&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await?&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        session_ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;register_table&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Dest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; initial_table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create source table meta&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; src_meta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; KVTableMeta&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1001&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Src&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]))),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            highest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exprs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Literal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;ScalarValue&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;())))],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Literal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;ScalarValue&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;())))],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;            vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Literal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;ScalarValue&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;())))],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; values_plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LogicalPlanBuilder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;values&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;exprs&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Column&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;column1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;alias&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create an insert plan to insert the source data into the initial table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; insert_into_table&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            LogicalPlanBuilder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;insert_into&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;values_plan&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Dest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Create a physical plan from the insert plan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; plan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_ctx&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;state&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;create_physical_plan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;insert_into_table&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Execute the physical plan and collect the results&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; res&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; collect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;plan&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;task_ctx&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;extract_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;res&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; expected_count&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; target_schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Field&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; DataType&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Utf8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ])));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; DBTableScanExec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1002&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;target_schema&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;initial_table&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;execute&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; session_ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;task_ctx&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stream&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.await.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;transpose&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;batch&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;exec_datafusion_err!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;unknown err when fetching batch from stream&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;	&#x2F;&#x2F;&#x2F; This function takes out the number from batches, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;	&#x2F;&#x2F;&#x2F; `res` should have only one row and one column.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; extract_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;res&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RecordBatch&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u64&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;res&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;expected one batch, got &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; res&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;res&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;num_columns&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;            1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;expected 1 column, got &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;num_columns&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; batch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;column&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_primitive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;UInt64Type&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;expected 1 row, got &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; col&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;had value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;expected non null&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        val&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;完整代码可以参考&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Kev1n8&#x2F;drdb&#x2F;commit&#x2F;62a552803d070be7db2381351013d021b99f6e1e&quot;&gt;这里&lt;&#x2F;a&gt;。&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>StringView in Apache Arrow &amp; a DataFusion SUBSTR Optimization</title>
        <published>2024-09-09T00:00:00+00:00</published>
        <updated>2024-09-09T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/stringview-in-apache-arrow-a-datafusion-substr-optimization/"/>
        <id>https://kev1n8.github.io/posts/stringview-in-apache-arrow-a-datafusion-substr-optimization/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/stringview-in-apache-arrow-a-datafusion-substr-optimization/">&lt;p&gt;Apache Arrow是一个高效的列式数据的内存表示，本文所指Arrow是arrow-rs，也就是Arrow的Rust实现，本文介绍其&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;与&lt;code&gt;StringArray&lt;&#x2F;code&gt;的不同，这也是Rust版本才有的特性。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stringarrayjian-jie&quot;&gt;StringArray简介&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;StringArray&lt;&#x2F;code&gt;是arrow的一种基本列实例、是一列字符串数据，一个&lt;code&gt;StringArray&lt;&#x2F;code&gt;主要有&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;一个&lt;code&gt;nullmap&lt;&#x2F;code&gt;，指示某一行是否为null&lt;&#x2F;li&gt;
&lt;li&gt;一个&lt;code&gt;offset&lt;&#x2F;code&gt;数组，指示某一行的字符串在Buffer上的位移&lt;&#x2F;li&gt;
&lt;li&gt;一个Buffer用来存储实际的UTF8字符串数据&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;yi-ge-stringviewchang-shen-me-yang&quot;&gt;一个StringView长什么样？&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;中同样也有&lt;code&gt;nullmap&lt;&#x2F;code&gt;，但是却不使用简单的&lt;code&gt;offset&lt;&#x2F;code&gt;，而是用一个&lt;code&gt;StringView&lt;&#x2F;code&gt;来指示字符串。首先，一个&lt;code&gt;StringView&lt;&#x2F;code&gt;的大小为16字节，前4个字节用来表示字符串长度。剩余的12字节分两种情况，&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;字符串编码字节数小于12时，该字符串直接内联到存放在剩余的12字节内&lt;&#x2F;li&gt;
&lt;li&gt;字节数大于12时，存放buffer编号、偏移和前缀，各占用4字节。其中，buffer编号即完整字符串数据所存放的，属于该&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;的buffer的编号；偏移即在该buffer中第几个字节开始为该字符串数据；前缀存放了字符串前4个字节的编码数据。&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;Stringview.png&quot; alt=&quot;Stringview&quot; &#x2F;&gt;{:height=&quot;360px&quot; width=&quot;auto&quot;}&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stringarray-vs-stringviewarray&quot;&gt;StringArray vs StringViewArray&lt;&#x2F;h2&gt;
&lt;p&gt;好处显而易见，如果一个Array中的字符串有大量重复，那么&lt;code&gt;StringView&lt;&#x2F;code&gt;就可以简单指向Buffer中的同一段数据，大大减少内存用量。如下面的示例，对于一个Array：&lt;&#x2F;p&gt;
&lt;p&gt;&quot;Apache StringView&quot;&lt;&#x2F;p&gt;
&lt;p&gt;&quot;Apache DataFusion&quot;&lt;&#x2F;p&gt;
&lt;p&gt;&quot;Apache StringView&quot;&lt;&#x2F;p&gt;
&lt;p&gt;&quot;InfluxDB&quot;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;能够重复利用此前出现过的字符串，相比&lt;code&gt;StringArray&lt;&#x2F;code&gt;，Buffer减少了17字节（另外这里可以看出在数据量小的情况，因为&lt;code&gt;StringView&lt;&#x2F;code&gt;占用的内存大于一般的&lt;code&gt;OffsetSize&lt;&#x2F;code&gt;，如&lt;code&gt;i32, i64&lt;&#x2F;code&gt;，&lt;code&gt;StringView&lt;&#x2F;code&gt;的好处较难体现）&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;StringViewDesign.png&quot; alt=&quot;StringViewDesign&quot; &#x2F;&gt;{:height=&quot;230px&quot; width=&quot;auto&quot;}&lt;&#x2F;p&gt;
&lt;h2 id=&quot;stringviewarrayru-he-gou-jian&quot;&gt;StringViewArray如何构建&lt;&#x2F;h2&gt;
&lt;p&gt;arrow提供了&lt;code&gt;StringViewArrayBuilder&lt;&#x2F;code&gt;来构建一个Array，而关键函数就是&lt;code&gt;append_value(&amp;amp;mut self, value: impl AsRef&amp;lt;T::Native&amp;gt;)&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; append_value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; AsRef&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Native&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; length&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;try_into&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; length&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; view_buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            view_buffer&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;copy_from_slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_le_bytes&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            view_buffer&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;copy_from_slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;views_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from_le_bytes&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;view_buffer&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;null_buffer_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append_non_null&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Deduplication if:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; (1) deduplication is enabled.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; (2) len &amp;gt; 12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ht&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;string_tracker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;take&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hash_val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;hash_one&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;hash_one&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; entry&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ht&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                hash_val&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stored_value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get_value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stored_value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                hasher_fn&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; entry&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                Entry&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Occupied&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;occupied&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; If the string already exists, we will directly use the view&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; idx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; occupied&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    self.&lt;&#x2F;span&gt;&lt;span&gt;views_builder&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;views_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_slice&lt;&#x2F;span&gt;&lt;span&gt;()[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;idx&lt;&#x2F;span&gt;&lt;span&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    self.&lt;&#x2F;span&gt;&lt;span&gt;null_buffer_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append_non_null&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    self.&lt;&#x2F;span&gt;&lt;span&gt;string_tracker &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;ht&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                    return&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;                Entry&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Vacant&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;vacant&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; o.w. we insert the (string hash -&amp;gt; view index)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                    &#x2F;&#x2F; the idx is current length of views_builder, as we are inserting a new view&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;                    vacant&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;views_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;string_tracker &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;ht&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; hasher&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; required_cap&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;in_progress&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if self.&lt;&#x2F;span&gt;&lt;span&gt;in_progress&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;capacity&lt;&#x2F;span&gt;&lt;span&gt;() &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; required_cap&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;flush_in_progress&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; to_reserve&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;max&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;block_size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;next_size&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;in_progress&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;reserve&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;to_reserve&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; offset&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;in_progress&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;in_progress&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;extend_from_slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ByteView&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            length&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            prefix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from_le_bytes&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;try_into&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            buffer_index&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: self.&lt;&#x2F;span&gt;&lt;span&gt;completed&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            offset&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;views_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;null_buffer_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append_non_null&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;大致逻辑如下：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;检查value大小，如果小于12，直接内联并制作一个view，使用成员&lt;code&gt;views_builder&lt;&#x2F;code&gt;加入views中&lt;&#x2F;li&gt;
&lt;li&gt;若大于12，则会使用哈希方法检查是否在buffer中已有该字符串，如有则直接根据所在位置制作一个view并添加&lt;&#x2F;li&gt;
&lt;li&gt;若不存在这个字符串，就会尝试添加进buffer，此处如果buffer达到指定大小，则会保存起来作为只读“历史buffer”，以便GC。&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;当然，这是从零构建一个&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;的方法，我们还可以直接通过&lt;code&gt;generic_view_array::new_unchecked&lt;&#x2F;code&gt;方法来不安全地直接构造一个Array，具体见下文。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;shi-yong-stringviewyou-hua-substrhan-shu&quot;&gt;使用StringView优化SUBSTR函数&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;substr(str, start, [count])&lt;&#x2F;code&gt;可以帮助我们获得字符串的指定字串，因为&lt;code&gt;substr&lt;&#x2F;code&gt;的结果一定是原字符串的子集，那么如果输入的&lt;code&gt;DataType&lt;&#x2F;code&gt;是&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;的话，我们就可以这样操作：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;直接将原本的所有buffer拷贝作为新Array的buffer，&lt;&#x2F;li&gt;
&lt;li&gt;然后直接操作每个&lt;code&gt;StringView&lt;&#x2F;code&gt;， 只需要根据通过计算得到的&lt;code&gt;sub_string&lt;&#x2F;code&gt;的index构建view并载入&lt;code&gt;views_buffer&lt;&#x2F;code&gt;中&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;这样做的好处是：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;使用&lt;code&gt;StringViewArray&lt;&#x2F;code&gt;带来了节约内存的好处&lt;&#x2F;li&gt;
&lt;li&gt;直接复制所有buffer相比通过此前&lt;code&gt;generic_array_builder&lt;&#x2F;code&gt;一次次调用&lt;code&gt;append_value&lt;&#x2F;code&gt;更高效。虽然buffer可能暂时会有无用的部份，但是GC可以后续把无用的部份回收掉。&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;下面列举一个示例函数用于手动收集view和构造&lt;code&gt;nullmap&lt;&#x2F;code&gt;以便在&lt;code&gt;new_unchecked&lt;&#x2F;code&gt;中使用：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; Make a `u128` based on the given substr, start(offset to view.offset), and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; push into to the given buffers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; make_and_append_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    views_buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    null_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; NullBufferBuilder&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    raw&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    substr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; substr_len&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; substr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sub_view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; substr_len&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ByteView&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;raw&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        make_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;substr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_bytes&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;buffer_index,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; view&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;offset &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; start&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; inline value does not need block id or offset&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        make_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;substr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_bytes&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    views_buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;sub_view&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    null_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;append_non_null&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; Somewhere else:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;	let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; views_buf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ScalarBuffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;views_buf&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; nulls_buf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; null_builder&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;finish&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Safety:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; (1) The blocks of the given views are all provided&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; (2) Each of the range `view.offset+start..end` of view in views_buf is within&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; the bounds of each of the blocks&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    unsafe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; array&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; StringViewArray&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new_unchecked&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; 在这里直接构造一个StringViewArray&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            views_buf&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            string_view_array&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;data_buffers&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_vec&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            nulls_buf&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;array&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ArrayRef&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;完整的优化代码可以参考&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;apache&#x2F;datafusion&#x2F;pull&#x2F;12044&quot;&gt;这里&lt;&#x2F;a&gt;。&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Distributed System Lab</title>
        <published>2024-07-20T00:00:00+00:00</published>
        <updated>2024-07-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/distributed-system-lab/"/>
        <id>https://kev1n8.github.io/posts/distributed-system-lab/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/distributed-system-lab/">&lt;h1 id=&quot;distributed-system-labs&quot;&gt;Distributed System Labs&lt;&#x2F;h1&gt;
&lt;p&gt;Some drafts and notes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-mapreduce&quot;&gt;#1 MapReduce&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;test-mr-sh&quot;&gt;test_mr.sh&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;mrcoordinator .&#x2F;&#x2F;pg*txt&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;then multiple workers&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;mrworker .&#x2F;&#x2F;.&#x2F;&#x2F;mrapps&#x2F;app.so&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Workflow&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;master awake, prepare rpc and splits. workers awake, prepare map&#x2F;reduce functions and start asking for task.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;master get M map tasks and R reduce tasks ready. picking up workers to assign&lt;&#x2F;li&gt;
&lt;li&gt;map workers read the splits and save the intermediate files, tell the master about the R locations(which files)&lt;&#x2F;li&gt;
&lt;li&gt;master receive task done msg, master picks workers to do reduce task, telling them the location of the R they are going to deal with.&lt;&#x2F;li&gt;
&lt;li&gt;reduce workers use rpc to read target R files from different map workers. once all read, sort them all by inter-key. reduce workers then iterate over the sorted data and do reduce func. the ouput of the func is appended to a final output file of this R partition. done, call master is done&lt;&#x2F;li&gt;
&lt;li&gt;once all done, master&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h4 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h4&gt;
&lt;p&gt;So I suppose that this is not that hard since it didn&#x27;t take much time for me to accomplish the task. To be honest, the reason I&#x27;d been stuck for a little while when &lt;code&gt;bash test-mr.sh&lt;&#x2F;code&gt; is because the path of the file is incorrect (changed it to absolute path later ).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-key-value-server&quot;&gt;#2 Key&#x2F;Value Server&lt;&#x2F;h2&gt;
&lt;p&gt;To implement an in-memory server and clients. It&#x27;s pretty easy that there&#x27;s really no much to talk about.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Techniques Used&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Still, using &lt;strong&gt;RPC&lt;&#x2F;strong&gt; to call remote functions&lt;&#x2F;li&gt;
&lt;li&gt;Using &lt;em&gt;unique Ids&lt;&#x2F;em&gt; to represent each of the clients and each request.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;strong&gt;2 Things blocked me for a while&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;A silly mistake - the fact is that &lt;code&gt;Call()&lt;&#x2F;code&gt; is executed only once, and then there&#x27;s a terrible infinite loop&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ok&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; :=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ck&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Call&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Server.Put&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;reply&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; !&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Using &lt;em&gt;Unique Ids&lt;&#x2F;em&gt; to represent &lt;code&gt;values&lt;&#x2F;code&gt; instead of Reqs - bad things would happen when other clients change the record, hence causing duplicate put&#x2F;append&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h4 id=&quot;conclusion-1&quot;&gt;Conclusion&lt;&#x2F;h4&gt;
&lt;p&gt;The lab is marked &lt;strong&gt;Easy&lt;&#x2F;strong&gt;, which is true just that I am supposed to finish it in real quick.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-raft&quot;&gt;#3 Raft&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;election&quot;&gt;Election&lt;&#x2F;h3&gt;
&lt;p&gt;I&#x27;ve drawn a figure listing out the summary of rules, check out:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;election_state.png&quot; alt=&quot;state_transfer&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Also, some variables and details as:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;election_variable&amp;amp;rules.png&quot; alt=&quot;election_variables_rules&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;log&quot;&gt;Log&lt;&#x2F;h3&gt;
&lt;p&gt;The rule of log replication can be interpreted as the following graphs:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;log_replication_basic.png&quot; alt=&quot;log_replication_basic&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Demonstrating an unreliable test that I&#x27;d debugged for a while and Tips:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;log_replication_atest.png&quot; alt=&quot;log_replication_atest&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In order to back up faster, I also implement the following technique:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;log_replication_quick_backup.png&quot; alt=&quot;log_replication_quick_backup&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;By sending &lt;code&gt;XTerm&lt;&#x2F;code&gt;, &lt;code&gt;XIndex&lt;&#x2F;code&gt;, and &lt;code&gt;Len&lt;&#x2F;code&gt; back to the leader, the leader can then quickly decide what &lt;code&gt;nextIndex[target]&lt;&#x2F;code&gt; should be set and get rid of meaningless retries.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;persistence&quot;&gt;Persistence&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;persistence_summary.png&quot; alt=&quot;persistence_summary&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;log-compaction&quot;&gt;Log Compaction&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;log_compaction_procedure.png&quot; alt=&quot;log_compaction_procedure&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;log_compaction_points.png&quot; alt=&quot;log_compaction_points&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-fault-tolerant-k-v-service&quot;&gt;#4 Fault-Tolerant K&#x2F;V Service&lt;&#x2F;h2&gt;
&lt;p&gt;Note: All of the fields in RPC args should be initialized, otherwise the RPC will fail.(on returning(never return))&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;lab4.png&quot; alt=&quot;lab4&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Summary of 445 Projects</title>
        <published>2024-05-10T00:00:00+00:00</published>
        <updated>2024-05-10T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/summary-of-445-projects/"/>
        <id>https://kev1n8.github.io/posts/summary-of-445-projects/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/summary-of-445-projects/">&lt;h1 id=&quot;summary-of-445-projects&quot;&gt;Summary of 445 Projects&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;project-0&quot;&gt;Project 0&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task1&quot;&gt;Task1&lt;&#x2F;h3&gt;
&lt;p&gt;首先，任务是实现Trie前缀树的Get、Put、Remove三种操作。和一般的树操作实现不同的是，这个Task要求&lt;strong&gt;Copy on Write&lt;&#x2F;strong&gt;。意思是说，在写操作时，获取Trie根结点的拷贝，然后在副本上面进行写操作，等写操作结束了以后再将它写回原本的Trie根。这么做的一个好处是读可以随时进行，因为写是在副本上的，在写完的那一个瞬间才会以类似”读“的形式写回去。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task2&quot;&gt;Task2&lt;&#x2F;h3&gt;
&lt;p&gt;并发控制，主要是两点：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;高级指针的使用，比如&lt;code&gt;std::unique_lock()&lt;&#x2F;code&gt;搭配互斥锁&lt;&#x2F;li&gt;
&lt;li&gt;资源访问时的上锁逻辑&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;task3&quot;&gt;Task3&lt;&#x2F;h3&gt;
&lt;p&gt;主要是调试和检查变量状态&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task4&quot;&gt;Task4&lt;&#x2F;h3&gt;
&lt;p&gt;两个函数的实现：根据Option对字符串进行大写或小写转换，以及在另一个cpp文件中注册并调用该函数&lt;&#x2F;p&gt;
&lt;h3 id=&quot;zong-jie&quot;&gt;总结&lt;&#x2F;h3&gt;
&lt;p&gt;Task1是占用时间最长的，主要原因是&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;对现代C++语法不太熟悉&lt;&#x2F;li&gt;
&lt;li&gt;不熟悉COW的实现，还总是想着修改结点，而不是Clone()&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;ul&gt;
&lt;li&gt;熟悉了C++的现代高级语法，比如&lt;strong&gt;锁&lt;&#x2F;strong&gt;和&lt;strong&gt;高级指针&lt;&#x2F;strong&gt;的使用&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;project-1&quot;&gt;Project 1&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task1-1&quot;&gt;Task1&lt;&#x2F;h3&gt;
&lt;p&gt;实现LRU-K算法，算是比较经典的题，第一次实现的时候，为了优先实现功能，于是直接给每一个函数都上了锁，等要Leaderboard挑战的时候再看怎么减小锁的粒度。&lt;&#x2F;p&gt;
&lt;p&gt;在本地实现都通过的情况下，Evict()一直过不了。后面发现，我的LRU-K在没有$k_{th}$引用记录的情况下，对overall最佳是采用LatestTimestamp，然而项目要求是Earliest。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task2-1&quot;&gt;Task2&lt;&#x2F;h3&gt;
&lt;p&gt;缓冲池管理实现。理解每个函数的要求以后做起来其实是比较轻松的。同样的，为了减少思考，第一次实现直接给整个函数上锁。十分顺利地通过大部份测试，除了&lt;code&gt;FetchPage&lt;&#x2F;code&gt;, &lt;code&gt;DelPage&lt;&#x2F;code&gt;, &lt;code&gt;IsDirty&lt;&#x2F;code&gt;和&lt;code&gt;Unpin&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;错误原因：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FetchPage&lt;&#x2F;code&gt;：从磁盘读取数据的函数用错了，用了&lt;code&gt;strcpy&lt;&#x2F;code&gt;。看了提供的测试代码，应该用&lt;code&gt;memcpy&lt;&#x2F;code&gt;。前者截断了&lt;code&gt;&#x27;\0&#x27;&lt;&#x2F;code&gt;，而后者原封不动地拷贝内存&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;IsDirty&lt;&#x2F;code&gt;：因为在&lt;code&gt;Unpin&lt;&#x2F;code&gt;直接给page的dirty标志赋值为参数所给值，如果page本来是脏但是参数不脏就会导致错误。正确做法应该是求异或&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;DelPage&lt;&#x2F;code&gt;：一开始只是简单调用了&lt;code&gt;ResetMemory&lt;&#x2F;code&gt;，元数据没有重新初始化，导致错误&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;此外，&lt;code&gt;Evict&lt;&#x2F;code&gt;进行后，要从&lt;code&gt;page_table&lt;&#x2F;code&gt;删除对应键值对，但是顺序错了。在删除后，&lt;code&gt;page_id&lt;&#x2F;code&gt;已经被设置成&lt;code&gt;INVALID&lt;&#x2F;code&gt;的情况下试图删除，造成的结果就是frame看似还在池里。这是最主要的错误，是上述错误的部份原因。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task3-1&quot;&gt;Task3&lt;&#x2F;h3&gt;
&lt;p&gt;页守卫的编写。最关键要理解它们的作用：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Basic：负责自动Unpin&lt;&#x2F;li&gt;
&lt;li&gt;Read：负责自动释放读锁和Unpin&lt;&#x2F;li&gt;
&lt;li&gt;Write：负责自动释放写锁和Unpin&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;这一节的代码量实际并不高，但是却在这里卡了许久。最后是让代码看上去更“规范”一点的情况下过了测试。猜测是因为一开始调用Drop()和处理要丢弃的项，导致锁可能没有顺利释放，造成其它线程永久等待。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;leaderboardtiao-zhan&quot;&gt;Leaderboard挑战&lt;&#x2F;h3&gt;
&lt;p&gt;TODO，在通过Leaderboard测试的前提下，官方提供了一个Buffer Pool的Benchmark。根据官方建议和我自己的想法，可能的优化方法有：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;前面提到的&lt;strong&gt;锁粒度减小&lt;&#x2F;strong&gt;，分别在LRU-K和buffer pool manager优化锁的使用&lt;&#x2F;li&gt;
&lt;li&gt;Benchmark有8个序列扫描Scan线程，8个随机获取Get线程，一共16个线程。在LRU-K算法层添加请求类别这一参数，优化算法使其在Evict的时候能作出&lt;strong&gt;更有效率的决定&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;并行化I&#x2F;O操作&lt;&#x2F;strong&gt;，其实就是1带来的好处&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;多线程编程，一定要注意函数内外是否会重复获取互斥锁，不然会死锁；还有就是操作的原子性，有的时候读操作用写锁是必须的，比如在Unpin之前的page检查。&lt;&#x2F;p&gt;
&lt;p&gt;完成情况：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;锁粒度减小，一开始简单地以为把每个共享资源保护起来就可以了（用完就释放，导致前后不一致，见&lt;strong&gt;TIP&lt;&#x2F;strong&gt;）。最后的做法是把每个函数切割成两部分，第一部分是涉及到bpm的共享数据结构的部份，需要所有线程共用一把互斥锁保证访问顺序。第二部分是对页面&lt;code&gt;pages_&lt;&#x2F;code&gt;的具体操作，使用了&lt;code&gt;pool_size_&lt;&#x2F;code&gt;个互斥锁，让每个页面得以并行运行。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;&#x2F;strong&gt;：最后看上去过了，后面其实修改了很久，因为多线程出错很随机，最后才终于想明白。突破口是bpm_bench.cpp文件，考虑到这里只涉及到NewPage, FetchPage和UnpinPage，而NewPage只是用来创建。分析思路：首先Fetch读到的page肯定是不能被篡改的，因为篡改的唯一途径是evict，而pin值不为0是不可能的，就是说正在读的page是不可能被篡改的。那么就考虑数据写入的时候数据就是错误的了。造成写入错误的可能有：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;page_id_错误&lt;&#x2F;li&gt;
&lt;li&gt;page_.data错误&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;此前的&lt;strong&gt;数据写入测试&lt;&#x2F;strong&gt;正常通过，所以判断是page_id_出错。我的代码是两段锁，但是这样保证不了前后读取page_id的一致性。于是我把锁的范围扩大，终于通过测试。从最后到达排位540，末尾水平。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;尝试了LRU-K添加一个lru链表（最后总体长得有点像倒排索引），位次提升到440。但是对于区分Scan和Get的算法优化，我只尝试了把Scan添加到lru中（LRUReplacer），位次到435。就在写这段话的时候，意识到在BPM层面还可以对Scan只进行以下个性化的Fetch&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;检查是否在pool，在的话不计数&lt;&#x2F;li&gt;
&lt;li&gt;不在的话，也就是history为空，加一个远古时间戳——0&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;上述操作能尽量让本次Scan仿佛没来过，如果遇到了连续的Scan，返回的也会是最近的Scan。&lt;&#x2F;p&gt;
&lt;p&gt;经过完善Fetch优化，提升到了259。题目在Unpin的参数还预留了一个Accesstype，暂时先放下了。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;zong-jie-1&quot;&gt;总结&lt;&#x2F;h3&gt;
&lt;p&gt;学到的东西：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;LRU-K的实现，全局时间戳&lt;&#x2F;li&gt;
&lt;li&gt;BPM实现，锁的覆盖范围怎么确定、unique_lock等的使用&lt;&#x2F;li&gt;
&lt;li&gt;Guard的基本原理，RAII技术、右值引用&lt;&#x2F;li&gt;
&lt;li&gt;互斥资源的保护&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;project-2-checkpoint-1&quot;&gt;Project 2 Checkpoint#1&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task1-2&quot;&gt;Task1&lt;&#x2F;h3&gt;
&lt;p&gt;简单来说，就是完善B+树的page基类、internal page和leaf page的一些函数。前期刚开始做的时候被源码的意图弄得有点糊涂，这几个类其实也是在做后面的Task时逐步完善的。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task2a&quot;&gt;Task2a&lt;&#x2F;h3&gt;
&lt;p&gt;完成&lt;code&gt;GetValue&lt;&#x2F;code&gt;和&lt;code&gt;Insert&lt;&#x2F;code&gt;函数，现在能想到的要注意的点有：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;GetValue&lt;&#x2F;code&gt;： 只需要自上而下遍历树到相应的叶子结点就行了。写的时候被方向guide变量坑到了，一开始没有意识到二分查找的值和guide应该的对应关系。好在后面顺利修复了。关键词：二分查找、guide&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Insert&lt;&#x2F;code&gt;：插入比较简单，只需要应对页的分裂的问题即可。但是做的时候状态不太好，索性照着教材的伪代码敲了，幸运的是没遇到什么bug，很快就通过了测试。中途SplitTest和ScaleTest没过（我想ScaleTest没过应该是Split的问题导致的）。到discord找线索，发现做B+树实现的人比哈希索引的人相对少好多。看到一个对于Split思路的hints，拷贝如下。总之按照这个思路过了一遍之后发现自己的Size调整有点问题，修改之后就过了。关键词：递归&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Make sure you follow this: &quot;However, you should correctly perform split if insertion triggers current number of key&#x2F;value pairs after insertion equals to max_size&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Leaf page : &lt;strong&gt;on&lt;&#x2F;strong&gt; successful insert, max size = actual size? Split it. In other words, the size of a leaf page is the number of values.&lt;&#x2F;p&gt;
&lt;p&gt;Internal page : &lt;strong&gt;before&lt;&#x2F;strong&gt; insert, max size = current size (aka current number of pointers)? Split it. In other words, the size of an internal page is the number of pointers &lt;strong&gt;including&lt;&#x2F;strong&gt; the next page pointer.&lt;&#x2F;p&gt;
&lt;p&gt;SplitTest is a pretty invasive test that unfortunately tests the implementation details (because it is hard not to). So your code might be right but the autograder might not accept it. Though more commonly, people make some error with MoveHalfTo or similar functions.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;补充：SplitTest后面因为更改了Split Leaf的写法（从先插入后split到先split后插，避免溢出），导致特殊情况（LeafMaxSize==2, InternalMaxSize==3的情况下没有处理好分裂操作，0位元素没有正确被剔除）下发生错误；另外，更改了刚刚说的先后顺序后，没有在最开始判断是否是已经存在的元素，导致元素会先被剔除后被判断，呈现的结果就是如下所示，&lt;code&gt;[1,2,3,4,5]&lt;&#x2F;code&gt;基础上再试图插入&lt;code&gt;[1,2]&lt;&#x2F;code&gt;，结果分裂为空：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)                &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)             (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ()    ()    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;zong-jie-2&quot;&gt;总结&lt;&#x2F;h3&gt;
&lt;p&gt;这次的Project难度还是挺大的，之前在课上了解的很多的优化方法，一到了实践，随着时间，我脑子里那些的优化理论都变成了最基本功能实现的编程细节。脚踏实地很重要。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;补充&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;并发测试因为页的的max_size比较大，因此测出了我的二分查找其实有点问题，会出错的情况是：已有key[1, 2, 3, 4, 6]，想要插入5，此时应该返回4
错误代码：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span&gt; high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;GetSize&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;  while&lt;&#x2F;span&gt;&lt;span&gt; (low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; high) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    int&lt;&#x2F;span&gt;&lt;span&gt; mid &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; (high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; low)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;comparator_&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;cmp_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;KeyAt&lt;&#x2F;span&gt;&lt;span&gt;(mid), key)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; 这一步会让返回的pos是第一个“不大于”目标的数&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;comparator_&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;cmp_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;KeyAt&lt;&#x2F;span&gt;&lt;span&gt;(mid), key)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; == -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;      break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;修正后：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span&gt; high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;GetSize&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;  while&lt;&#x2F;span&gt;&lt;span&gt; (low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; high) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    int&lt;&#x2F;span&gt;&lt;span&gt; mid &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; (high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; low)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;comparator_&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;cmp_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;KeyAt&lt;&#x2F;span&gt;&lt;span&gt;(mid), key)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      high &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F; 收敛更“收敛”一点，这样能确保返回的pos是应该插入的位置&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;comparator_&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;cmp_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;page&lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;KeyAt&lt;&#x2F;span&gt;&lt;span&gt;(mid), key)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; == -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      low &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; mid;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;      break&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;project-2-checkpoint-2&quot;&gt;Project 2 Checkpoint#2&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task2b&quot;&gt;Task2b&lt;&#x2F;h3&gt;
&lt;p&gt;树的删除操作，本次project最难。最开始是想着按照官网提示的Roadmap自己写出伪代码，但是脑子转不太动。后面在YouTube上找到一个B+树删除操作的讲解视频，讲解非常清晰，很快理解了核心思路，最后自己把Delete完成了。同时也是最耗费时间的Task。绝大部分时间都花在调试上，出现错误的点为：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;根节点特别判断没有做好，具体是：前一个if条件判断要求是根节点，后一个if（不是else if）又没有要求不是根节点，导致根节点被当作普通parent进行merge&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;==完整理解删除的两大操作：&lt;code&gt;Merge&lt;&#x2F;code&gt;和&lt;code&gt;Borrow&lt;&#x2F;code&gt;以后，这个操作从难以理解变为了比较难落实。==&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task3-2&quot;&gt;Task3&lt;&#x2F;h3&gt;
&lt;p&gt;Iterator实现，和前面的操作比起来过于简单。值得注意的点：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;End的判定，到底怎么样才算End？&lt;&#x2F;li&gt;
&lt;li&gt;++操作，怎么判断到没到底？到底了怎么办？&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;task4-1&quot;&gt;Task4&lt;&#x2F;h3&gt;
&lt;p&gt;利用Project1的PageGuard非常好实现，只要注意写锁和读锁的使用和释放位置就行。&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;但是并发MixTest一直无法通过，涉及到的log都是说对应key不存在。&lt;&#x2F;p&gt;
&lt;p&gt;我自己测试了多达10000个key的插入，得到的树看似非常正常。再试了一下1000个key，发现key的顺序被打乱，！！！原来是我的&lt;code&gt;BorrowFromInternal&lt;&#x2F;code&gt;在向右sibling借的时候，我的实现只满足了&lt;code&gt;max_internal_size&lt;&#x2F;code&gt;为2的情况。&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;最后只剩并发Insert2和Mix1过不了，索性整个Insert函数上锁，结果通过了。于是确定只是锁的问题。&lt;&#x2F;p&gt;
&lt;p&gt;Contention一直过不了。在并发Insert时偶尔发生Segementation fault (具体是在InsertLeaf和InsertParent)，有时发生死锁。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;死锁排查过程：&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;想到死锁可能发生的情况：buffer pool满了而且每个页都pinned，这时候如果请求HeaderPage而且池子里没有HeaderPage的话，就会锁住？不是。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;找到Segmentation Fault，是由于latch crabbing的时候，条件判断错误了，只因我最开始将InternalPage的Size设计成key的数量，很混乱，总之错了+1+2的问题。现在还差ContentionBenchmark会发生死锁。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;通过打印大量log，我发现，在两个线程的情况下，每次死锁前都是一个线程先输出FetchWrite后输出Fetch header page txn: xx。问题是这时候没人拥有锁啊！为什么会死锁呢？&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;原来死锁来自同一线程重复请求header page，第二次请求来自&lt;code&gt;next_page_id&lt;&#x2F;code&gt;（从根到叶的途中），理论上这不可能发生。发现是因为buffer pool manager没有将此前&lt;code&gt;NewPageGuard&lt;&#x2F;code&gt;得到的page flush到磁盘（通过打印log验证确实没有），导致可能获取一个空页，进而导致死锁。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;问题确认了，是BPM的并发控制没有做好。试着给BPM上一把全局锁，所有问题都消失了。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;bpm-dirty-page-eviction.png&quot; alt=&quot;铁证&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;上图：page12663没有Write page to disk就再次被请求&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;问题来了，BPM出现了什么问题？见下文。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;zong-jie-3&quot;&gt;总结&lt;&#x2F;h3&gt;
&lt;p&gt;这次实验真的花费了我大量的时间和精力，而其中大部份都花费在debug上。而出现的问题大多又是一些特殊情况的判断和边界细节处理上。总之就是，以后出现问题，优先考虑程序在&lt;strong&gt;特殊情况&lt;&#x2F;strong&gt;下的表现。最难的操作：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Borrow 哪个key被借走，借到哪里，哪些key要删除&lt;&#x2F;li&gt;
&lt;li&gt;Merge 搞清楚哪些key被删除，哪个key被上传&lt;&#x2F;li&gt;
&lt;li&gt;Split 主要是leaf可能会溢出，需要特殊照顾&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                             (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;)                                      &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)                (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;)                      (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;)              &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; d4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; p&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                   (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;)                                   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)                (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;)                    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;)            &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)    (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;leaderboard-tiao-zhan&quot;&gt;Leaderboard 挑战&lt;&#x2F;h3&gt;
&lt;p&gt;虽然做完了，但是排名特别低，不是很满意，写个乐观锁吧。&lt;&#x2F;p&gt;
&lt;p&gt;然而实现后才发现问题并不简单。遇到了和之前类似的问题，脏page不写回！但是后来修改了一些代码，复现不了了，甚至加了Flush也不管用。&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;BPM大锁+B+无手动FLUSH，可过Contention ： 4.39和1.46&lt;&#x2F;li&gt;
&lt;li&gt;BPM原样+B+手动FlushNewPage，可过Contention：5.35和1.1&lt;&#x2F;li&gt;
&lt;li&gt;BPM大锁+乐观锁，不可过Contention：0.62和&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;最大疑点：BPM在Evict的时候为什么不Write脏页？&lt;&#x2F;p&gt;
&lt;p&gt;最后终于找到原因：虽然每个页都上锁了，但是这只能保证页在写入时之间是互不干扰的，读没有保证。例如：&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;线程A&lt;&#x2F;strong&gt;：evict其他某个页，然后读取page 1&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;线程B&lt;&#x2F;strong&gt;：出于evict，要刷page 1脏，已经标记page#1不在buffer中&lt;&#x2F;p&gt;
&lt;p&gt;现在，线程B正在执行，但是还没有开始刷脏，同时A开始读取page 1。虽然他们都获取了锁，但是锁是各自的target（buffer_pool对应槽位，而不是page_id），线程A如果比B先完成Evict操作，那么A就会尝试从磁盘读page1，但是此时磁盘的page1不是最新的，因为线程B还没有刷脏完成。这就导致前面的疑点发生，事实上，并非是Evict了但是没有write，而是还没write就Read了。还没来得及打印write结束，程序就终止或死锁了。&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;对于这个问题，做完所有Projects后，我现在有两个可以解决的想法：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;在脏页成功刷盘之前不要把&lt;code&gt;table_map_&lt;&#x2F;code&gt;的锁释放，这样其它线程就会在刷盘完成了以后才会去试图从磁盘FetchPage&lt;&#x2F;li&gt;
&lt;li&gt;参考P4的做法，为写evict这个操作配置一个请求队列，每个page_id都有一个队列，读者来了可以读尾部（最新）的page，每个page写入过程可以另起线程，并行I&#x2F;O&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;至于为什么乐观锁出错，出错场景展示在下面。推测插入顺序是7、6、8&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;猜想&lt;&#x2F;strong&gt;：&lt;code&gt;8&lt;&#x2F;code&gt;申请了&lt;code&gt;[6, 7]&lt;&#x2F;code&gt;页的写锁，而这个写锁目前正在被一个线程插入&lt;code&gt;9&lt;&#x2F;code&gt;。页发生分裂，变成&lt;code&gt;[6], [7, 9]&lt;&#x2F;code&gt;，这时&lt;code&gt;8&lt;&#x2F;code&gt;才获得&lt;code&gt;[6]&lt;&#x2F;code&gt;的写锁，但是这个页已经不是&lt;code&gt;8&lt;&#x2F;code&gt;应该插入的了。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;optimistic-lock-error.png&quot; alt=&quot;乐观锁错误原因&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;在获取写锁之前提前判断一下大小，相当于双重判断size是否符合，结果测试通过了。然后试了一下把获取写锁以后的大小判断去掉，也可以。原来是我的条件判断位置错了。&lt;&#x2F;p&gt;
&lt;p&gt;再次印证了那句话：&lt;em&gt;线程只有拿到锁以后数据才是安全的&lt;&#x2F;em&gt;。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;还原一下两种判断位置的&lt;em&gt;区别&lt;&#x2F;em&gt;：&lt;&#x2F;strong&gt; 如果是获取写锁才判断，很显然会发生上面提到的错误。然而，如果读锁期间判断，由于本线程还握着读锁，可能有其他线程正在等待写锁，那么这时我提前判断，就避免了因为没有马上拿到写锁而导致页不一样的问题。因为如果页会分裂，我在读锁就保证了信息全面。但是如果有两个或者以上写锁在等待呢？&lt;&#x2F;p&gt;
&lt;p&gt;这样就通过了，但是分数反而低了。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;project-3&quot;&gt;Project 3&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task-1&quot;&gt;Task#1&lt;&#x2F;h3&gt;
&lt;p&gt;实现以下Executor：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;SeqScan&lt;&#x2F;p&gt;
&lt;p&gt;要注意判断是否被标记为已删除&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Insert&lt;&#x2F;p&gt;
&lt;p&gt;注意插入index的key是怎么获取的（通过key_schema锁定当前index涉及的列）&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Update&lt;&#x2F;p&gt;
&lt;p&gt;同样，要注意处理index，先删除后插入&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Delete&lt;&#x2F;p&gt;
&lt;p&gt;Update的子集&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;IndexScan&lt;&#x2F;p&gt;
&lt;p&gt;通过获取此前完成的IndexIterator来获取，值得注意的是发现并完善了此前边界情况&lt;code&gt;End()&lt;&#x2F;code&gt;一些不太好的处理，比如初始化是&lt;code&gt;INVALID_PAGE_ID&lt;&#x2F;code&gt;会导致报错等等&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;坑点：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;insert, update, delete操作都是一次完成，输出的tuple是发生变动的row数&lt;&#x2F;li&gt;
&lt;li&gt;注意首次Next必须返回True尽管可能得到的tuple是大小为0&lt;&#x2F;li&gt;
&lt;li&gt;注意index需要根据它的key_schema进行插入key，不能直接将整个tuple插入index&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;task-2&quot;&gt;Task#2&lt;&#x2F;h3&gt;
&lt;p&gt;最耗时的就是看懂源码和调试，但是一旦理解了源码原理，上手就很快&lt;&#x2F;p&gt;
&lt;p&gt;完成聚合和加入：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Aggregation&lt;&#x2F;p&gt;
&lt;p&gt;在Init的阶段把所有来自child的数据flow截断并使用hash计算和存储要求的结果，在Next迭代返回&lt;&#x2F;p&gt;
&lt;p&gt;头文件的实现学到了把&lt;strong&gt;相同hash值根据不同情况（如count每次+1、sum每次+input[i]、minmax记录最大最小）合并计算&lt;&#x2F;strong&gt;的方法&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;NestedLoopJoin&lt;&#x2F;p&gt;
&lt;p&gt;使用两层循环迭代来自child的tuple，值得注意的是为了保存进度，使用左右成员变量记录当前位置。如果能一次读完所有tuple一次性返回会简单很多&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;HashJoin&lt;&#x2F;p&gt;
&lt;p&gt;参考Aggregation实现了JoinKey和JoinValue，不友好的一点是不能直接用来调试，因为还没完成优化器实现&lt;&#x2F;p&gt;
&lt;p&gt;优化器实现后发现漏了哈希冲突处理，完善策略使用了简单的线性探测&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;NLJasHashJoin&lt;&#x2F;p&gt;
&lt;p&gt;实现将NestedJoin转换成HashJoin的优化器，凡事&lt;code&gt;=&lt;&#x2F;code&gt;条件的Join都能转换成HashJoin&lt;&#x2F;p&gt;
&lt;p&gt;这里只要求实现至多一个&lt;code&gt;and&lt;&#x2F;code&gt;的情况，如果要实现多个and都能处理应该写一个子程序对expression二叉树进行处理，把所有左和右（表）叶子结点（ColumnValueExpression）的列都拼接到一起，然后基于此构建一个新的HashPlanNode继承原有NestJoinPlanNode的children，特别注意ColumnExpression是左表还是右表，PlanNode倒是默认都是分好的&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;task-3&quot;&gt;Task#3&lt;&#x2F;h3&gt;
&lt;p&gt;实现以下功能：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Sort&lt;&#x2F;p&gt;
&lt;p&gt;由于是内存内的sort，直接收集（阻塞操作）所有tuple，然后对它们进行排序即可&lt;&#x2F;p&gt;
&lt;p&gt;排序需要实现一个自定义的排序函数，结合order_by所给的线索进行排序&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Limit&lt;&#x2F;p&gt;
&lt;p&gt;Limit的实现更加直接维护一个计数器，在计数器达到限定的tuple数就可以返回false了&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Top-N优化&lt;&#x2F;p&gt;
&lt;p&gt;简单来说，就是如果出现了&lt;code&gt;order by xxx limit n&lt;&#x2F;code&gt;的语句，不要把所有tuple保存完再统一排序，而是一边读取一边排序，同时丢弃超过容量的tuple&lt;&#x2F;p&gt;
&lt;p&gt;因为上面实现了HashJoin，这里的优化规则实现起来就比较熟悉了&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;总体来说，做了Task#2再来做这里会觉得轻松很多，也有可能是因为对代码理解更深了，知道每个部份是在做什么。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;leaderboard&quot;&gt;Leaderboard&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;q1&quot;&gt;Q1&lt;&#x2F;h4&gt;
&lt;p&gt;利用Index的有序性，对where操作进行优化，包括where的列是index主键子集的情况。&lt;&#x2F;p&gt;
&lt;p&gt;初步分析，对于where是index列的子集的情况，可以按以下步骤：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;根据FilterPlan找出条件&lt;code&gt;condition&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;遍历Index，找出包含&lt;code&gt;condition&lt;&#x2F;code&gt;所有列的index，key列少的优先（利用率大）&lt;&#x2F;li&gt;
&lt;li&gt;因为index是&lt;strong&gt;递增&lt;&#x2F;strong&gt;的，只需要找到左侧起点（要么从头，要么从某个点）开始遍历B+树，==在遇到第一个不符合&lt;code&gt;predicate&lt;&#x2F;code&gt;的表明结束，返回false即可。==&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;目测难点在于找到左侧起点&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;完成后总结&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;上面的分析基本正确，需要注意：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;“第一个不符合predicate就表明结束”这一结论，不适用于&lt;code&gt;x&amp;gt;=90 and y=10&lt;&#x2F;code&gt;中&lt;code&gt;y&lt;&#x2F;code&gt;的情况，很显然这种情况下应该返回的分布不是连续的，所以没办法提前锁定结束位置，但是以下这三种情况：&lt;code&gt;x&amp;gt;, x&amp;gt;=, &amp;lt;x, &amp;lt;=x&lt;&#x2F;code&gt;可以帮助我们锁定起点，跳转到最早的可用点。&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;q2&quot;&gt;Q2&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;strong&gt;谓词下推&lt;&#x2F;strong&gt;，就是把在join后的Filter移动到join前，减少Join的工作量&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;decomposing_predicate.png&quot; alt=&quot;decomposing_predicate&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;初步分析：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;找到Filter跟随Join的情况&lt;&#x2F;li&gt;
&lt;li&gt;分解Filter的predicate，把它的col分配给Join的左右子结点&lt;&#x2F;li&gt;
&lt;li&gt;如果子结点是Scan，直接把predicate赋值给它&lt;&#x2F;li&gt;
&lt;li&gt;如果子结点是Join，那么赋值给它之后，再次对它调用本函数&lt;&#x2F;li&gt;
&lt;li&gt;两个子结点执行完后，就应该会得到右图。注意Filter并非实际存在，而是以&lt;code&gt;predicate&lt;&#x2F;code&gt;的形式存在&lt;code&gt;PlanNode&lt;&#x2F;code&gt;中&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;strong&gt;完成后总结&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;首先，弄清楚自己在做什么真的很重要。根据图来理解，事半功倍，一些实现时的细节：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Join一定会在Join的左子结点，知道这一点可以简化实现时的一些操作&lt;&#x2F;li&gt;
&lt;li&gt;正确地判断Filter中的列是属于Join左边的那个Filter还是右边的（通过获取左表的列数判断）&lt;&#x2F;li&gt;
&lt;li&gt;将Join上方的Filter“肢解”给子结点后，分别继续对子结点递归调用本函数，因为子结点也有可能是Join&lt;&#x2F;li&gt;
&lt;li&gt;Expression树有可能会有Logic, Comparison, ColumnValue和Constant，应对策略分别如下：
&lt;ul&gt;
&lt;li&gt;Logic，子结点可能是Comparison或Constant，直接将它们分别传入本函数&lt;&#x2F;li&gt;
&lt;li&gt;Comparison，子结点可能是ColumnValue或Constant，对于前者，根据它的&lt;code&gt;ColIdx&lt;&#x2F;code&gt;决定是要把当前Comparison放到“左边”还是“右边”。Comparison本身应该被视为叶子结点&lt;&#x2F;li&gt;
&lt;li&gt;有时Logic的子结点会是Constant结点，什么都不用做，不要放到“左边”或者“右边”&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;最终得到的新的predicate会是3个，分别是要给左Filter、右Filter和自己的Filter的，值得注意的是predicate有可能是空的，那么需要配置一个true Filter&lt;&#x2F;li&gt;
&lt;li&gt;Merge Filter操作放到这个优化规则后(&lt;code&gt;MergeFilterScan&lt;&#x2F;code&gt;, &lt;code&gt;MergeFilterNLJ&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;q3&quot;&gt;Q3&lt;&#x2F;h4&gt;
&lt;p&gt;Q3的测试用例包含很多奇怪的SQL，其中穿插了一个永远为false的条件，这会导致SeqScan无意义地循环，必须提前制止。此外，还有很多无用的聚集函数，也需要优化掉。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;实现后总结&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;要实现的有：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;IsPredicateFalse&lt;&#x2F;code&gt;检查函数&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;OptimizeDropFalseScan&lt;&#x2F;code&gt;替换永为false的那层executer为dummy scan（空的ValuesPlan）&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;FindUsefulColumnsforAgg&lt;&#x2F;code&gt;传入Expression，根据这个Expression树寻找实际需要的&lt;code&gt;col_id&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;DropUselessAgg&lt;&#x2F;code&gt;通过检查双层Projection，确认没有必要的聚合函数计算，只保留有需要的部份&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;重点总结一下&lt;code&gt;IsPredicateFalse&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;考虑三种结点：Logic、Comparison和ConstantValue&lt;&#x2F;li&gt;
&lt;li&gt;Logic只能是And或Or，只需要递归判断它子结点是否为永False并统计，结合And和Or就能知道是否为永否&lt;&#x2F;li&gt;
&lt;li&gt;Comparison的子结点有ColumnValue和ConstantValue两种，对应的情况有1==2, 1!=1和v1!=v1这三种永否&lt;&#x2F;li&gt;
&lt;li&gt;ConstantValue被认为直接反应这个Filter的predicate就是永真或永否，直接返回相反值（比如&lt;code&gt;ConstantValue==false&lt;&#x2F;code&gt;，说明永否，返回&lt;code&gt;true&lt;&#x2F;code&gt;）即可&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;再说说&lt;code&gt;DropUselessAgg&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;规则考虑的是双层Projection，最后一层是前面一层的子集&lt;&#x2F;li&gt;
&lt;li&gt;首先压缩两层projection为一层：把不需要的从子层去掉，把需要的提取出来替换到上层，只保留上层&lt;&#x2F;li&gt;
&lt;li&gt;对于此时的Projection层，&lt;code&gt;expressions_&lt;&#x2F;code&gt;即子集，寻找有效&lt;code&gt;col_id&lt;&#x2F;code&gt;，也就是在Aggregation层要保留的&lt;&#x2F;li&gt;
&lt;li&gt;步骤简述：
&lt;ol&gt;
&lt;li&gt;中间结点为ArithmeticExpression，叶子结点为ColumnValueExpression（当然也可能是ConstantValue，但是没有用处）&lt;&#x2F;li&gt;
&lt;li&gt;中间结点则递归子结点&lt;&#x2F;li&gt;
&lt;li&gt;遇到ColumnValue，记录这个column的&lt;code&gt;col_id&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;得到有用的&lt;code&gt;col_id&lt;&#x2F;code&gt;以后，就可以通过&lt;code&gt;agg_plan&lt;&#x2F;code&gt;的schema中的&lt;code&gt;column_&lt;&#x2F;code&gt;找到有用的列，通过这些列的名称又可以找到聚合函数（agg开头e.g. &lt;code&gt;agg#1&lt;&#x2F;code&gt;）&lt;&#x2F;li&gt;
&lt;li&gt;最终利用上面的信息，就可以构建一个简化的plan了&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;不足之处：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;只考虑了两层Projection的情况&lt;&#x2F;li&gt;
&lt;li&gt;得到最后的&lt;code&gt;col_id&lt;&#x2F;code&gt;等信息后，没有将它们向前“靠拢”，虽然这似乎不会导致运行错误，因为虽然数值不是连续的，但是它们都是对应的&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;group_by_&lt;&#x2F;code&gt;没有去掉无用的key&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;结果表现&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;优化完成后，Q1表现最佳，排名到了13；Q2和Q3分别排33和36位。如果前面BPM的并行化I&#x2F;O有实现的话应该还能再高一点。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;project-4&quot;&gt;Project 4&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;task-1-1&quot;&gt;Task#1&lt;&#x2F;h3&gt;
&lt;p&gt;实现LockManager，具体包括对Table和Row的Lock和Unlock。&lt;&#x2F;p&gt;
&lt;p&gt;最开始按照笔记自己写了一遍，但代码结构混乱，评测不通过。于是按照官方的函数框架重写了一遍，比较顺利地通过了测试。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;最初的实现想法&lt;&#x2F;em&gt;：&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;first_idea_queue.png&quot; alt=&quot;first_idea_queue&quot; &#x2F;&gt;{:height=&quot;600px&quot; width=&quot;auto&quot;}&lt;&#x2F;p&gt;
&lt;p&gt;要注意的点：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;按照笔记提示完成函数&lt;&#x2F;li&gt;
&lt;li&gt;Lock顺序简单描述如下：
&lt;ol&gt;
&lt;li&gt;事务是否能请求锁（根据隔离级别&lt;&#x2F;li&gt;
&lt;li&gt;请求row锁要额外检查是否已经在表上添加了对应的锁&lt;&#x2F;li&gt;
&lt;li&gt;检查自己是否已经持有锁，如果是低级的锁，标记升级；如果是持有更高级的锁，或者是不匹配的升级，中止事务&lt;&#x2F;li&gt;
&lt;li&gt;现在是否有事务在等待，是则wait；否则跳过沉睡&lt;&#x2F;li&gt;
&lt;li&gt;唤醒后检查是否可以获取锁，具体为先查看自己是否为下一批目标事务，然后检查当前队列已获得锁的请求与自己是否兼容&lt;&#x2F;li&gt;
&lt;li&gt;若都满足，走出wait循环；否则继续wait。每次唤醒后还要在循环体内检查是否已经被abort了，是的话要将自己的请求从队列中删除并&lt;strong&gt;广播一次唤醒其他可能在等待中且符合条件的事务&lt;&#x2F;strong&gt;，最后返回false&lt;&#x2F;li&gt;
&lt;li&gt;赋予事务锁，更新事务的book，修改granted为true，完成&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Unlock顺序简单描述如下：
&lt;ol&gt;
&lt;li&gt;检查是否真的有锁&lt;&#x2F;li&gt;
&lt;li&gt;根据隔离级别修改事务状态&lt;&#x2F;li&gt;
&lt;li&gt;table锁要额外检查是否还有row锁没释放&lt;&#x2F;li&gt;
&lt;li&gt;更新事务book&lt;&#x2F;li&gt;
&lt;li&gt;从队列中删除当前事务的请求&lt;&#x2F;li&gt;
&lt;li&gt;唤起下一波事务&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;多线程编程还是那句话，只有拿到了锁之后的数据才是可信的！&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;可以改进的点&lt;&#x2F;strong&gt;：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;因为事务每次被唤醒会调用IsAllowedToGrant函数，这个函数的做法是遍历当前队列，检查兼容性（隔离）和优先性（FIFO）是否同时满足。对于其中的兼容性，可以通过&lt;strong&gt;维护一个highest变量&lt;&#x2F;strong&gt;记录当前锁的最高等级，所有不与它兼容的锁在它释放（然后更新highest）前都不能被授予。这也是我最开始的实现。但是实现可能出错了，为了确保正确性还是先用循环实现了。&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;task-2-1&quot;&gt;Task#2&lt;&#x2F;h3&gt;
&lt;p&gt;死锁检测实现，还算比较好实现。简单来说就是有一个背景线程持续地根据当前的队列情况建立一个Graph，使用DFS检测环的存在，剔除&lt;strong&gt;最年轻&lt;&#x2F;strong&gt;的事务。&lt;&#x2F;p&gt;
&lt;p&gt;顺序简述：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;首先，通过当前所有的请求队列记录等待的&lt;strong&gt;边&lt;&#x2F;strong&gt;，形成Graph&lt;&#x2F;li&gt;
&lt;li&gt;waitings --&amp;gt; holdings，而且是互不兼容的锁才会形成等待边&lt;&#x2F;li&gt;
&lt;li&gt;获得的waits_for_ map存放了所有边，将所有事务id排序存放，从lowest开始DFS（为了test的正确性），map的second（vector）中的元素也是顺序存放的&lt;&#x2F;li&gt;
&lt;li&gt;找到环了以后，找出最大id的事务，然后将这个事务的请求从所有队列中删除并标记为aborted，再次找环，直到没有环了为止。&lt;&#x2F;li&gt;
&lt;li&gt;清除所有环后，在所有队列上尝试授予下一波事务&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;这里卡了一段时间，倒不是因为很难，而是它有一个GetEdgeList函数要返回所有边，来检测我的正确性。我是用txn_mgr来获取所有事务id的，而test只是给waits_for_专门赋值了而已，并没有真正事务存在。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;task-3-1&quot;&gt;Task#3&lt;&#x2F;h3&gt;
&lt;p&gt;逻辑并发控制！简单来说，就是Task#1完成了锁的功能，现在要来用这些锁了。要求的其实很简单，只需要满足SeqScan、Insert和Delete这三个executor就可以。&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;SeqScan：最主要的。有一个IsDelete()的Context上下文函数，提示是否需要持&lt;strong&gt;排他&lt;&#x2F;strong&gt;的锁。&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;对于Table（&lt;code&gt;Init&lt;&#x2F;code&gt;函数），拿到oid后，先检查是否必要加锁，因为可能已经有了更高级的锁。&lt;&#x2F;li&gt;
&lt;li&gt;对于Row（&lt;code&gt;Next&lt;&#x2F;code&gt;函数），拿到rid后，同样先检查是否必要加锁。此外，因为实现了predicate push down，如果这个row不符合条件或者是已经被删除的，要再将它Force Unlock掉，因为事务不需要这个row。&lt;&#x2F;li&gt;
&lt;li&gt;最后，根据隔离级别选择性的释放S锁&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Insert：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;在&lt;code&gt;Init&lt;&#x2F;code&gt;函数对表上IX锁（？）&lt;&#x2F;li&gt;
&lt;li&gt;在&lt;code&gt;Next&lt;&#x2F;code&gt;函数记录加入的row到write_set，以便后续对abort事务进行回滚&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Delete：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;如果在SeqScan是通过IsDelete()判断并提前上了排他锁，这里在&lt;code&gt;Init&lt;&#x2F;code&gt;函数就不需要上锁了&lt;&#x2F;li&gt;
&lt;li&gt;同样在&lt;code&gt;Next&lt;&#x2F;code&gt;函数记录删除的row，以便回滚&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;一定是要拿到锁，再Fetch资源！&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;然后就是两个事务终结函数：Commit和Abort&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Commit：没什么要做的，只需要把事务所有持有的锁释放即可（根据book）&lt;&#x2F;li&gt;
&lt;li&gt;Abort：根据之前记录wrtie_set_，对其元素倒序执行，就可以实现回滚。&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;leatherboard&quot;&gt;Leatherboard&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;hint1&quot;&gt;Hint1&lt;&#x2F;h4&gt;
&lt;p&gt;Predicate pushdown to SeqScan，这个在P3就实现了，提交后比最低有3到5的提升。好处是减少SeqScan上传的元组数，进而减少上锁的row数。&lt;&#x2F;p&gt;
&lt;h4 id=&quot;hint2&quot;&gt;Hint2&lt;&#x2F;h4&gt;
&lt;p&gt;Implement In-Place UpdateExecutor，因为之前UPDATE操作是用DELETE+INSERT实现的，如果update一步到位，想必会快很多吧！但是这个要改的话涉及到B+树的原地操作，直接贯穿了3个Project！而且我第一时间也没把握能顺利完成。遂遗憾放弃。&lt;&#x2F;p&gt;
&lt;h4 id=&quot;hint3&quot;&gt;Hint3&lt;&#x2F;h4&gt;
&lt;p&gt;Use Index，好巧，这个我在P3也实现了。但是这里有一点不同：因为key不会重复，所以如果predicate是&lt;code&gt;CompareType::Equal&lt;&#x2F;code&gt;的话（e.g. 只有一个=），就可以&lt;strong&gt;获取一个tuple（若有）后确信后面再没有匹配的row，可以提前退出&lt;&#x2F;strong&gt;。但是可惜的是，运行bench的时候出现了死锁。而且是latch死锁而不是lock死锁。&lt;&#x2F;p&gt;
&lt;p&gt;现象有二：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;DeleteExecutor在执行DeleteEntry的时候Fetch B+树的根结点页发生死锁，理论上不应该发生的，怀疑是线程两次获取了这个页面。&lt;&#x2F;li&gt;
&lt;li&gt;注释掉DelteExecutor关于更新Index的操作后，程序在调试时不会发生死锁（快速点击其实还是会），在直接运行下会发生死锁，怀疑在别处还有死锁。&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;原因推测：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;线程在运行到索引的Remove函数必定发生死锁，很大概率是这个线程获取两次根结点的问题。&lt;&#x2F;li&gt;
&lt;li&gt;一开始以为是在IndexScan，树的Iter在最底部从左往右走；而在Delete和Insert，锁从上到下，获取锁的方向不一致（写到一半，发现这个不是原因。因为基于Hint3本身的实现，iter不会向右移动，但仍然会出现死锁）说明程序在别的地方还有其它重复两次获取同样的锁&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;zong-jie-4&quot;&gt;总结&lt;&#x2F;h3&gt;
&lt;p&gt;至此，所有Project都已经完成。感谢Andy将这门课程公开，让我有机会接触到在本校无法学到的知识；感谢TA们打造了bustub并在discord上解答非CMU学生的问题；也感谢自己的坚持。&lt;&#x2F;p&gt;
&lt;p&gt;一些&lt;strong&gt;可改进&lt;&#x2F;strong&gt;的点集合：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;BPM并行I&#x2F;O实现&lt;&#x2F;li&gt;
&lt;li&gt;B+树结点可在满size前借给邻居，减少连续地分裂&lt;&#x2F;li&gt;
&lt;li&gt;执行计划还可以有更多优化&lt;&#x2F;li&gt;
&lt;li&gt;Update操作原地进行&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Notes of Git</title>
        <published>2023-12-01T00:00:00+00:00</published>
        <updated>2023-12-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/notes-of-git/"/>
        <id>https://kev1n8.github.io/posts/notes-of-git/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/notes-of-git/">&lt;h1 id=&quot;notes-of-git&quot;&gt;Notes of Git&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;basic-settings&quot;&gt;Basic Settings&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;git&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; user.name&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;kvn&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;git&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; user.email username@email.com&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;git&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; core.autocrlf input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; # for MacOS&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;appending-operations&quot;&gt;Appending Operations&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Showing files in the staging area by &lt;code&gt;git ls-files&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Storing files that are &lt;code&gt;git add&lt;&#x2F;code&gt; .&lt;&#x2F;li&gt;
&lt;li&gt;Modified files will have to be &lt;code&gt;git add&lt;&#x2F;code&gt; again&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;If &lt;code&gt;git status&lt;&#x2F;code&gt; gives green info, it means it&#x27;s all the same for &lt;strong&gt;Working Directory&lt;&#x2F;strong&gt; and &lt;strong&gt;Staging Area&lt;&#x2F;strong&gt;. If it says committed, then &lt;strong&gt;Repository&lt;&#x2F;strong&gt; too.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Skip Staging&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git commit -am &quot;info&quot;&lt;&#x2F;code&gt; for adding&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git rm filename1...&lt;&#x2F;code&gt; for deleting&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git mv file file1&lt;&#x2F;code&gt; for renaming or moving&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Only do this when you know what you are doing&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ignoring-files&quot;&gt;Ignoring Files&lt;&#x2F;h2&gt;
&lt;p&gt;Files like logs usually shouldn&#x27;t be tracked.&lt;&#x2F;p&gt;
&lt;p&gt;use .gitignore file to book files unneeded in the following form:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;logs&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main.log&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;*.log&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If files are already in repo, adding them to .gitignore file is not going to let git ignore them. To solve this, we use &lt;code&gt;git rm --cached dir_dont_want_to_track&lt;&#x2F;code&gt; to only remove it from the staging area(may have to use &lt;code&gt;-r&lt;&#x2F;code&gt; to recursively &lt;code&gt;rm&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;viewing-operation&quot;&gt;Viewing Operation&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;simple-view&quot;&gt;Simple View&lt;&#x2F;h3&gt;
&lt;p&gt;use &lt;code&gt;git status -s&lt;&#x2F;code&gt; for simplified info.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;M&lt;&#x2F;code&gt; for modified; &lt;code&gt;A&lt;&#x2F;code&gt; for added.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;see-diff&quot;&gt;See Diff&lt;&#x2F;h3&gt;
&lt;p&gt;use &lt;code&gt;git diff --staged&lt;&#x2F;code&gt; to compare files changed between the last committed and currently staged.&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git diff&lt;&#x2F;code&gt; to compare files changed between the currently staged and working directory.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;diff-tool&quot;&gt;Diff Tool&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Set up&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;git config --global diff.tool vscode&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;git config --global difftool.vscode.cmd &quot;code --wait --diff $LOCAL $REMOTE&quot;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;check by:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;git config --global -e&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;history&quot;&gt;History&lt;&#x2F;h3&gt;
&lt;p&gt;use &lt;code&gt;git log&lt;&#x2F;code&gt; to see history.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Options&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;--oneline&lt;&#x2F;code&gt;: simplified output, ID &amp;amp; comment&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;--reverse&lt;&#x2F;code&gt;: presented in the way of bottom up&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;use &lt;code&gt;git show ID&lt;&#x2F;code&gt; or &lt;code&gt;git show HEAD~step_before&lt;&#x2F;code&gt; to see the diff of the commits&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git show ID:file&lt;&#x2F;code&gt; to see the file of the commit &lt;code&gt;ID&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git ls-tree ID&lt;&#x2F;code&gt; to list all files with their &lt;code&gt;unique_id&lt;&#x2F;code&gt;  of version &lt;code&gt;ID&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git show unique_id&lt;&#x2F;code&gt; to see &lt;code&gt;blob&lt;&#x2F;code&gt; or &lt;code&gt;tree&lt;&#x2F;code&gt; by their &lt;code&gt;unique_id&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;restoring-files&quot;&gt;Restoring Files&lt;&#x2F;h2&gt;
&lt;p&gt;use &lt;code&gt;git restore --staged file&lt;&#x2F;code&gt; to restore files from staging area to the working directory&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;when restoring modified files, git uses copies from the repo to replace files in the staging area.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;when restoring added files, git reverts the stage of files back to untracked&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;use &lt;code&gt;git restore file&lt;&#x2F;code&gt; to restore files from the working directory to the previous version&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;when restoring modified files, git replaces them with the latest copies from the repo&lt;&#x2F;li&gt;
&lt;li&gt;git doesn&#x27;t restore untracked files, if no &lt;strong&gt;Options&lt;&#x2F;strong&gt; like &lt;code&gt;-fd&lt;&#x2F;code&gt; are given, the move is considered dangerous and will not process.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;use &lt;code&gt;git restore --source=HEAD~1 file&lt;&#x2F;code&gt; to restore files to the version &lt;code&gt;ID&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;remote&quot;&gt;Remote&lt;&#x2F;h2&gt;
&lt;p&gt;use &lt;code&gt;git remote add name url&lt;&#x2F;code&gt; to add new remote repo&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git pull&lt;&#x2F;code&gt; to directly pull the repo to the working directory(space)&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git fetch&lt;&#x2F;code&gt; to fetch the repo to the local repo&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git push&lt;&#x2F;code&gt; to push the local repo to the remote&lt;&#x2F;p&gt;
&lt;h2 id=&quot;branches&quot;&gt;Branches&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;basics&quot;&gt;Basics&lt;&#x2F;h3&gt;
&lt;p&gt;use &lt;code&gt;git branch name&lt;&#x2F;code&gt; to create a new branch from the current branch&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git branch switch name&lt;&#x2F;code&gt; to switch HEAD to branch name&lt;&#x2F;p&gt;
&lt;h3 id=&quot;merge-or-rebase&quot;&gt;Merge or Rebase&lt;&#x2F;h3&gt;
&lt;p&gt;The following graph presents 3 different ways of merging branches.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;git-merge-rebase.png&quot; alt=&quot;Git merge and rebase comparison&quot; &#x2F;&gt;{:height=&quot;400px&quot; width=&quot;auto&quot;}&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git merge feature&lt;&#x2F;code&gt; to merge the feature branch to the master when HEAD -&amp;gt; master&lt;&#x2F;p&gt;
&lt;p&gt;use &lt;code&gt;git rebase feature&lt;&#x2F;code&gt; to merge the branch in terms of rebase to the master&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dealing-conflicts&quot;&gt;Dealing Conflicts&lt;&#x2F;h3&gt;
&lt;p&gt;when two branches change the same line of the same file and attend to merge, one has to decide which version to stay.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Notes of MySQL</title>
        <published>2023-07-13T00:00:00+00:00</published>
        <updated>2023-07-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/notes-of-mysql/"/>
        <id>https://kev1n8.github.io/posts/notes-of-mysql/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/notes-of-mysql/">&lt;h1 id=&quot;mysql&quot;&gt;MySQL&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;sql&quot;&gt;SQL&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;ddl&quot;&gt;DDL&lt;&#x2F;h3&gt;
&lt;p&gt;数据定义语言 Data Definition Language&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;show databases;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select database0;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;create database [if not exists] base_name [default charset] [collate];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drop database [if exists] base_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use base_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;show tables;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;desc table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;show create table;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;create table table_name(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		field_1 field_type [comment cmt],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  	...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)[comment cmt];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alter table table_name modify target_field new_type(len);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alter table table_name change old_field new_field new_type(len) [comment cmt] [restrain];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alter table table_name drop field;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alter table table_name rename to new_table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drop table [if exists] table_name;#删目录&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;truncate table table_name;#删内容&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;dml&quot;&gt;DML&lt;&#x2F;h3&gt;
&lt;p&gt;数据操作语言 Data Manipulation Language&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;insert into table_name(field1,...) values(val1,...);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;insert into table_name values(val...);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;insert into table_name(field1,...) values(val1,...),...;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;insert into table_name values(val...),...;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;update table_name set field1=val1,... [where condition];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;delete from table_name [where condition];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;dql&quot;&gt;DQL&lt;&#x2F;h3&gt;
&lt;p&gt;数据查询语言 Data Query Language&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;基本查询&lt;&#x2F;li&gt;
&lt;li&gt;条件查询(where)&lt;&#x2F;li&gt;
&lt;li&gt;聚合查询(count, max, min, avg, sum)&lt;&#x2F;li&gt;
&lt;li&gt;分组查询(group by)&lt;&#x2F;li&gt;
&lt;li&gt;排序查询(order by)&lt;&#x2F;li&gt;
&lt;li&gt;分页查询(limit)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select field1,... from table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select * from table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select field [[as] niknme],... from table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select distinct field from table_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from table_name where condition;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from table_name in (val1,...);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from table_name between ... and ...;#[]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from table_name like &amp;#39;_?&amp;#39; or &amp;#39;%?&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from ... [where con] group by evidence_field [hanving con];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from ... order by field1 asc_desc,...;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select ... from ... limit start_index, following_amount;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;dcl&quot;&gt;DCL&lt;&#x2F;h3&gt;
&lt;p&gt;数据控制语言 Data Control Language&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;use mysql;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;select * from user;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;create user &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39; identified by &amp;#39;pwd&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alter user &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39; identified with old_pwd by &amp;#39;new_one&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drop user &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#************************#&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;show grants for &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;grant grant_list on base.table to &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;revoke grant_list on base.table from &amp;#39;username&amp;#39;@&amp;#39;host&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;han-shu&quot;&gt;函数&lt;&#x2F;h2&gt;
&lt;p&gt;字符串函数&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Concat(s1,...)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Lower(str)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Upper(str)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;LPad(str,n,pad)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;RPad(str,n,pad)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Trim(str)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Substring(str, start,len)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;数值函数&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Ceil(x)&lt;&#x2F;li&gt;
&lt;li&gt;Floor(x)&lt;&#x2F;li&gt;
&lt;li&gt;Mod(x,y) x&#x2F;y的模&lt;&#x2F;li&gt;
&lt;li&gt;Rand()&lt;&#x2F;li&gt;
&lt;li&gt;Round(x,y) 四舍五入，保留y位小数&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;日期函数&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Curdate()&lt;&#x2F;li&gt;
&lt;li&gt;Curtime()&lt;&#x2F;li&gt;
&lt;li&gt;Now()&lt;&#x2F;li&gt;
&lt;li&gt;Year(date)&lt;&#x2F;li&gt;
&lt;li&gt;Month(date)&lt;&#x2F;li&gt;
&lt;li&gt;Day(date)&lt;&#x2F;li&gt;
&lt;li&gt;Date_add(date,INTERVAL expr type)&lt;&#x2F;li&gt;
&lt;li&gt;Datediff(date1,date2)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;流程函数&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;if(value t, f)&lt;&#x2F;li&gt;
&lt;li&gt;Ifnull(value1, value2)&lt;&#x2F;li&gt;
&lt;li&gt;Case when [val1] then [res1] ... else [default] end&lt;&#x2F;li&gt;
&lt;li&gt;Case [expr] then [res1] ... else [default] end&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;yue-shu&quot;&gt;约束&lt;&#x2F;h2&gt;
&lt;ol&gt;
&lt;li&gt;非空 not null&lt;&#x2F;li&gt;
&lt;li&gt;唯一 unique&lt;&#x2F;li&gt;
&lt;li&gt;主键 primary key (AUTO_INCREMENT)&lt;&#x2F;li&gt;
&lt;li&gt;默认 default&lt;&#x2F;li&gt;
&lt;li&gt;检查 check&lt;&#x2F;li&gt;
&lt;li&gt;外键 foreign key&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;duo-biao-cha-xun&quot;&gt;多表查询&lt;&#x2F;h2&gt;
&lt;p&gt;关系&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;一对多：多的一方设置外键，关联另一方主键&lt;&#x2F;li&gt;
&lt;li&gt;多对多：建立中间表，中间表包含两个外键，关联两张表主键&lt;&#x2F;li&gt;
&lt;li&gt;一对一：用于表结构拆分，任意表中设置唯一外键，关联另一表主键&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;查询&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;内连接&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;隐式：select ... from A,B where ...&lt;&#x2F;li&gt;
&lt;li&gt;显式：select ... from A inner join b on ...&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;外连接&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;左外：select ... from A left join B on ...&lt;&#x2F;li&gt;
&lt;li&gt;右外：select ... from A right join B on ...&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;自连接&lt;&#x2F;p&gt;
&lt;p&gt;Select ... from A a1,A a2 where ...&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;子查询&lt;&#x2F;p&gt;
&lt;p&gt;标量子查询、列子查询、行子查询、表子查询&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;shi-wu&quot;&gt;事务&lt;&#x2F;h2&gt;
&lt;p&gt;@@autocommit=0 手动提交事务&lt;&#x2F;p&gt;
&lt;p&gt;commit 提交事务&lt;&#x2F;p&gt;
&lt;p&gt;rollback 回滚&lt;&#x2F;p&gt;
&lt;p&gt;事务的四大特性&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;原子性 Atomicity&lt;&#x2F;li&gt;
&lt;li&gt;一致性 Consistency&lt;&#x2F;li&gt;
&lt;li&gt;隔离性 Isolation&lt;&#x2F;li&gt;
&lt;li&gt;持久性 Durability&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;事务并发存在的问题&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;脏读：读另一个事务没有提交的数据&lt;&#x2F;li&gt;
&lt;li&gt;不可重复读：先后读一个记录，但是数据不同（期间有其他事务提交）&lt;&#x2F;li&gt;
&lt;li&gt;幻读：无法插入&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;隔离级别&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Read uncommitted&lt;&#x2F;li&gt;
&lt;li&gt;Read commited&lt;&#x2F;li&gt;
&lt;li&gt;Repeatable Read&lt;&#x2F;li&gt;
&lt;li&gt;Serializable&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Select @@transaction_isolation&lt;&#x2F;p&gt;
&lt;p&gt;set [session|global] transaction isolation level {level1234}&lt;&#x2F;p&gt;
&lt;h2 id=&quot;suo&quot;&gt;锁&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;quan-ju-suo&quot;&gt;全局锁&lt;&#x2F;h3&gt;
&lt;p&gt;加锁后，整个数据库实例就处于只读状态，典型场景：全库逻辑备份&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;flush tables with read lock;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;mysqldump -uroot -p密码 basename &amp;gt; base.sql&lt;&#x2F;code&gt; 终端执行&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;unlock tables&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;备份期间，所有业务停摆&lt;&#x2F;li&gt;
&lt;li&gt;如果在库上备份，那么在备份期间不能从库执行主库同步过来的二进制日志会导致主从延迟&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;在InnoDB引擎中，可以在备份时加上参数--single-transaction来完成不加锁的一致性数据备份&lt;&#x2F;p&gt;
&lt;h3 id=&quot;biao-ji-suo&quot;&gt;表级锁&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;biao-suo&quot;&gt;表锁&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;表共享读锁 read lock&lt;&#x2F;li&gt;
&lt;li&gt;表独占写锁 write lock&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;code&gt;lock tables name read&#x2F;write&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;unlock tables &lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;读锁阻塞写，写锁阻塞所有操作。&lt;&#x2F;p&gt;
&lt;h4 id=&quot;yuan-shu-ju-suo-meta-data-lock-mdl&quot;&gt;元数据锁 meta data lock, MDL&lt;&#x2F;h4&gt;
&lt;p&gt;由系统自动控制，无需显式使用&lt;&#x2F;p&gt;
&lt;p&gt;事务A，B开启，操作对象表t&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;A select t&lt;&#x2F;li&gt;
&lt;li&gt;B select lock_info&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;table_lock_1.png&quot; alt=&quot;Screenshot 2023-07-12 at 10.19.00&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;B insert bob into employee&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;table_lock_2.png&quot; alt=&quot;image-20230712102406447&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;A select * from employee&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;table_lock_3.png&quot; alt=&quot;Screenshot 2023-07-12 at 10.25.28&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;A insert id3&lt;&#x2F;li&gt;
&lt;li&gt;B alter id to primary key (blocked) exclusive与其他不兼容&lt;&#x2F;li&gt;
&lt;li&gt;A commit;&lt;&#x2F;li&gt;
&lt;li&gt;B throws error, &#x27;cause A insert id3, primary key cannot be add&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h4 id=&quot;yi-xiang-suo&quot;&gt;意向锁&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;意向共享锁 IS &lt;code&gt;select ... lock in share mode&lt;&#x2F;code&gt;添加&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;​		与表锁共享锁兼容（read），与表锁排他锁（write）互斥&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;意向排他锁 IX &lt;code&gt;insert、update、delete、select ... for update&lt;&#x2F;code&gt;添加&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;​		与所有表锁互斥，意向锁之间不排斥&lt;&#x2F;p&gt;
&lt;p&gt;当表已经存在意向锁时试图加互斥表锁，会导致加表锁的事务线程阻塞，直到意向锁解除&lt;&#x2F;p&gt;
&lt;h3 id=&quot;xing-ji-suo&quot;&gt;行级锁&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;xing-suo-record-lock&quot;&gt;行锁 Record Lock&lt;&#x2F;h4&gt;
&lt;p&gt;锁定单行的纪录，防止其他事务update和delete&lt;&#x2F;p&gt;
&lt;p&gt;共享锁 S 排他锁 X 与之前类似&lt;img src=&quot;&#x2F;images&#x2F;Record_lock.png&quot; alt=&quot;Screenshot 2023-07-12 at 10.51.57&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;InnoDB使用next-key锁进行搜索和索引扫描，以防止幻读&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;针对唯一索引进行检索时，对已经存在的纪录进行等值匹配时，将自动优化为行锁&lt;&#x2F;li&gt;
&lt;li&gt;InnoDB的行锁是针对于索引加的锁，不通过索引条件检索数据，那么InnoDB将对所有记录加锁，此时升级为&lt;strong&gt;表锁&lt;&#x2F;strong&gt;。（SQL优化update）&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h4 id=&quot;jian-xi-suo-gaplock&quot;&gt;间隙锁 GapLock&lt;&#x2F;h4&gt;
&lt;p&gt;锁定索引记录的间隙，不包含行记录，确保间隙不变，防止其他事务insert，不同事务的间隙锁可以兼容&lt;&#x2F;p&gt;
&lt;p&gt;InnoDB使用next-key锁扫描，&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;索引上的等值查询（唯一索引），给不存在的纪录加锁时优化为间隙锁&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: center&quot;&gt;id&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;name&lt;&#x2F;th&gt;&lt;th style=&quot;text-align: center&quot;&gt;age&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;3&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;PHP&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;3&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center&quot;&gt;8&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;Java&lt;&#x2F;td&gt;&lt;td style=&quot;text-align: center&quot;&gt;4&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;A：&lt;code&gt;update table set age = 10 where id = 5;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;B：&lt;code&gt;insert into table values(7,Python,7);&lt;&#x2F;code&gt; (blocked)&lt;img src=&quot;&#x2F;images&#x2F;gap_lock.png&quot; alt=&quot;Screenshot 2023-07-14 at 10.59.57&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h4 id=&quot;lin-jian-suo-next-key-lock&quot;&gt;临键锁 Next-Key Lock&lt;&#x2F;h4&gt;
&lt;p&gt;行锁和间隙锁的结合，同时锁住数据和间隙&lt;&#x2F;p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;索引上的等值查询（普通索引），遍历到最后一个值不满足查询需求时，next-key lock退化为间隙锁&lt;&#x2F;li&gt;
&lt;li&gt;索引上的范围查询（唯一索引），会访问到不满足的条件的第一个值为止&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;锁的范围表示，间隙锁范围左开右闭。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;innodbyin-qing&quot;&gt;InnoDB引擎&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;luo-ji-cun-chu-jie-gou&quot;&gt;逻辑存储结构&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;storing_structure.png&quot; alt=&quot;点击查看图片来源&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h4 id=&quot;biao-kong-jian-ibd&quot;&gt;表空间 ibd&lt;&#x2F;h4&gt;
&lt;p&gt;一个mysql实例可以对应多个表空间，用于存储记录、索引等数据&lt;&#x2F;p&gt;
&lt;h4 id=&quot;duan-segment&quot;&gt;段 segment&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;Leaf node segment 数据段&lt;&#x2F;li&gt;
&lt;li&gt;Non-leaf node segment 索引段&lt;&#x2F;li&gt;
&lt;li&gt;Rollback segment 回滚段&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;InnoDB是索引组织表，数据段就是B+树的叶子结点，索引段为非叶子结点。段用来管理多个Extent&lt;&#x2F;p&gt;
&lt;h4 id=&quot;qu-extent&quot;&gt;区 extent&lt;&#x2F;h4&gt;
&lt;p&gt;表空间的单元结构，每个区大小为1M。默认下InnoDB存储引擎页大小为16K，即一个区中一共有64个连续的页&lt;&#x2F;p&gt;
&lt;h4 id=&quot;ye-page&quot;&gt;页 page&lt;&#x2F;h4&gt;
&lt;p&gt;InnoDB存储引擎管理磁盘的最小单元，每页大小默认16KB。为保证页的连续性，InnoDB每次从磁盘申请4-5个区&lt;&#x2F;p&gt;
&lt;h4 id=&quot;xing-row&quot;&gt;行 row&lt;&#x2F;h4&gt;
&lt;p&gt;InnoDB数据是按行存放的&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Trx_id 每次对某条记录进行改动时，都会把对应的事务id赋值给trx_id隐藏列。&lt;&#x2F;li&gt;
&lt;li&gt;Roll_pointer 每次对某条记录进行改动时，都会把旧的版本写入到undo日志中，如何隐藏列就相当于一个指针可以用来找到该记录修改前的信息&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;jia-gou&quot;&gt;架构&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;db_structure.png&quot; alt=&quot;Screenshot 2023-07-17 at 08.56.06&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h4 id=&quot;nei-cun-jie-gou&quot;&gt;内存结构&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;缓冲池 主内存的一个区域
&lt;ul&gt;
&lt;li&gt;单位 Page
&lt;ul&gt;
&lt;li&gt;free 空闲未被使用&lt;&#x2F;li&gt;
&lt;li&gt;clean 被使用但没修改过&lt;&#x2F;li&gt;
&lt;li&gt;dirty 数据被修改过&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;自适应哈希索引
&lt;ul&gt;
&lt;li&gt;用于优化对缓冲池数据的查询，InnoDB会监控对表上各索引页的查询，如果观察到hash索引可以提升速度，则建立hash索引&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;更改缓冲区
&lt;ul&gt;
&lt;li&gt;存储变更的数据，之后再同步到缓冲池，最后刷新到磁盘&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;日志缓冲区
&lt;ul&gt;
&lt;li&gt;保存要写入到磁盘中的log日志数据（redo log、undo log），默认大小16MB，日志缓冲区的日志回定期刷新到磁盘中。如果需要更新、插入或删除许多行的事务，增加日志缓冲区的大小可以节省磁盘I&#x2F;O&lt;&#x2F;li&gt;
&lt;li&gt;innodb_log_buffer_size 缓冲区大小&lt;&#x2F;li&gt;
&lt;li&gt;innodb_flush_log_at_trx_commit 刷新到磁盘时机
&lt;ul&gt;
&lt;li&gt;1 日志每次事务提交时写入并刷新到磁盘&lt;&#x2F;li&gt;
&lt;li&gt;0 每秒写入并刷新到磁盘一次&lt;&#x2F;li&gt;
&lt;li&gt;2 日志在每次事务提交后写入并每秒刷新到磁盘一次？？？？&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;ci-pan-jie-gou&quot;&gt;磁盘结构&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;System Tablespace
&lt;ul&gt;
&lt;li&gt;系统表空间，更改缓冲区的存储区域&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;File-Per-Table Tablespaces
&lt;ul&gt;
&lt;li&gt;独立表空间，每个表的文件表空间包含单个InnoDB表的数据和索引，并存储在文件系统上的单个数据文件中&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;General Tablespace
&lt;ul&gt;
&lt;li&gt;通用表空间，需要通过create tablespace创建，在创建表时，可以指定该表空间 e.g. Create tablespace xxx add datafile xxx engine = engine_name&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Undo Tablespaces
&lt;ul&gt;
&lt;li&gt;撤销表空间，MySQL实例在初始化时会自动创建两个默认的undo表空间（初始大小16M），用于存储undo日志&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Temporary Tablespaces
&lt;ul&gt;
&lt;li&gt;临时表空间，使用临时表空间和全局临时表空间存储用户创建的临时表&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Doublewrite Buffer Files
&lt;ul&gt;
&lt;li&gt;双写缓冲区，innoDB将数据页从缓冲池刷新到磁盘前，先将数据页写入双写缓冲区文件中，便于系统异常时恢复数据&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Redo Log
&lt;ul&gt;
&lt;li&gt;重做日志，用来实现事务持久性，该日志文件由两部分组成：重做日志缓冲（redo log buffer）以及重做日志文件（redo log），前者在内存中，后者在磁盘中。当事务提交之后会把所有修改信息都存到该日志中，用于刷新脏页到磁盘中、发生错误时，进行数据恢复使用&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;hou-tai-xian-cheng&quot;&gt;后台线程&lt;&#x2F;h4&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Master Thread&lt;&#x2F;p&gt;
&lt;p&gt;核心后台线程，负责调度其他线程，还负责将缓冲池的数据异步刷新到磁盘中，保持数据一致性，包括脏页刷新、合并插入缓存、undo页的回收&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;IO Thread&lt;&#x2F;p&gt;
&lt;p&gt;在InnoDB中大量使用了AIO来处理IO请求，这样可以极大地提高数据库性能，而IO Thread主要负责这些IO请求的回调&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Read thread 4&lt;&#x2F;li&gt;
&lt;li&gt;Write thread 4&lt;&#x2F;li&gt;
&lt;li&gt;Log thread 1&lt;&#x2F;li&gt;
&lt;li&gt;Insert buffer thread 1&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Purge Thread&lt;&#x2F;p&gt;
&lt;p&gt;主要用于回收事务已经提交了的undo log&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Page Cleaner Thread&lt;&#x2F;p&gt;
&lt;p&gt;协助Master Thread刷新脏页到磁盘的线程，可以减轻Master Thread的工作压力，减少阻塞&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h4 id=&quot;shi-wu-yuan-li&quot;&gt;事务原理&lt;&#x2F;h4&gt;
&lt;ul&gt;
&lt;li&gt;事务是一组操作的集合，是不可分割的工作单位，这些操作要么同时成功要么同时失败。&lt;&#x2F;li&gt;
&lt;li&gt;特性ACID&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;特性的实现&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;redo log和undo log&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;原子性&lt;&#x2F;li&gt;
&lt;li&gt;一致性&lt;&#x2F;li&gt;
&lt;li&gt;持久性&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;锁和MVCC&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;隔离性&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h5 id=&quot;redo-log&quot;&gt;redo log&lt;&#x2F;h5&gt;
&lt;p&gt;重做日志，记录事务提交时数据页的物理修改，用于实现事务的持久性。&lt;&#x2F;p&gt;
&lt;p&gt;文件由两部分组成，重做日志缓冲和重做日志文件。当事务提交后，会把所有修改信息都存到该日志文件中，用于在刷新脏页到磁盘&lt;strong&gt;发生错误&lt;&#x2F;strong&gt;时进行数据恢复&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;WAL write-ahead logging&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h5 id=&quot;undo-log&quot;&gt;undo log&lt;&#x2F;h5&gt;
&lt;p&gt;回滚日志，记录数据被修改前的信息，作用包括提供回滚和MVCC&lt;&#x2F;p&gt;
&lt;p&gt;undo log不同于redo log，属于逻辑日志，其中记录的是实际操作的反向操作，如delete操作会使文件中增加insert记录。&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;undo log销毁：事务执行时产生，事务提交时不会立即删除undo log，因为这些日志可能还用于MVCC&lt;&#x2F;li&gt;
&lt;li&gt;undo log存储：undo log采用段的方式进行管理和记录，存放在rollback segment回滚段中，内部包含1024个undo log segment&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h5 id=&quot;mvcc&quot;&gt;MVCC&lt;&#x2F;h5&gt;
&lt;p&gt;Multi-Version Concurrency Control&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;当前读&lt;&#x2F;p&gt;
&lt;p&gt;读取的是记录的最新版本，读取时还要保证其他并发事务不能修改当前记录，会对当前读取的记录进行加锁。select...lock in share mode, selcet... for update, update, insert, delete都是当前读&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;快照读&lt;&#x2F;p&gt;
&lt;p&gt;简单的select（不加锁）就是快照读，读到的是数据的历史版本，有可能是历史数据，不加锁是非阻塞读&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Read Committed 每次select都生成一个快照读&lt;&#x2F;li&gt;
&lt;li&gt;Repeatable Read 开启事务后第一个select语句才是快照读的地方&lt;&#x2F;li&gt;
&lt;li&gt;Serializable 快照读退化为当前读&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;MVCC——多版本并发控制，指维护一个数据的多个版本，使得读写操作没有冲突，快照读为MySQL实现MVCC提供了一个非阻塞读功能。MVCC具体实现依赖数据库记录中的&lt;strong&gt;三个隐式字段&lt;&#x2F;strong&gt;、&lt;strong&gt;undo log&lt;&#x2F;strong&gt;、&lt;strong&gt;realView&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h6 id=&quot;yin-cang-zi-duan&quot;&gt;隐藏字段&lt;&#x2F;h6&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;DB_TRX_ID&lt;&#x2F;p&gt;
&lt;p&gt;最近修改事务ID，记录插入这条记录或最后一次修改改记录的事务ID&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;DB_ROLL_PTR&lt;&#x2F;p&gt;
&lt;p&gt;回滚指针，指向这条记录的上一个版本，用于配合undo log指向上一版本&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;DB_ROW_ID&lt;&#x2F;p&gt;
&lt;p&gt;隐式主键，表结构本身没有主键会生成&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h6 id=&quot;undo-log-1&quot;&gt;undo log&lt;&#x2F;h6&gt;
&lt;p&gt;回滚日志，当insert的时候，产生的undo log只在回滚时需要，事务提交后可被立即删除；而update、delete时，产生的undo log不仅在回滚时需要，在快照读时也需要，不会被立即删除。&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;undo log版本链&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;undolog_chain.png&quot; alt=&quot;Screenshot 2023-07-13 at 11.08.59&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h6 id=&quot;readview&quot;&gt;readview&lt;&#x2F;h6&gt;
&lt;p&gt;readview（读视图）是快照读执行时MVCC提取数据的依据，记录并维护系统当前活跃的事务（未提交）的id&lt;&#x2F;p&gt;
&lt;p&gt;readview包含四个核心字段：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;m_ids 当前活跃事务id集合&lt;&#x2F;li&gt;
&lt;li&gt;min_trx_id 最小活跃事务ID&lt;&#x2F;li&gt;
&lt;li&gt;max_trx_id 预分配事务ID，即当前最大事务ID+1&lt;&#x2F;li&gt;
&lt;li&gt;creator_trx_id readview创建者的事务ID&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;版本链数据访问规则：trx_id代表当前版本事务ID&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;trx_id == creator_trx_id 可以访问 （说明数据是当前事务更改的&lt;&#x2F;li&gt;
&lt;li&gt;trx_id &amp;lt; min_trx_id 可以访问 （说明数据已经提交了&lt;&#x2F;li&gt;
&lt;li&gt;trx_id &amp;gt; max_trx_id 不可以访问 （说明事务是在readview生成后才开启&lt;&#x2F;li&gt;
&lt;li&gt;min_trx_id &amp;lt;= trx_id &amp;lt;= max_trx_id 若trx_id不在m_ids中是可以访问的 （说明数据已经提交&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;不同的隔离级别，生成readview的时机不同：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Read Committed 在事务中每次执行快照读时生成readview&lt;&#x2F;li&gt;
&lt;li&gt;Repeatable Read 仅在事务中第一次执行快照读时生成readview，后续复用该readview&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;结合&lt;strong&gt;undo log&lt;&#x2F;strong&gt;例子和&lt;strong&gt;readview&lt;&#x2F;strong&gt;规则理解&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Notes of Rust</title>
        <published>2023-04-24T00:00:00+00:00</published>
        <updated>2023-04-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://kev1n8.github.io/posts/notes-of-rust/"/>
        <id>https://kev1n8.github.io/posts/notes-of-rust/</id>
        
        <content type="html" xml:base="https://kev1n8.github.io/posts/notes-of-rust/">&lt;h2 id=&quot;ke-bian-he-bu-ke-bian-bian-liang&quot;&gt;可变和不可变变量&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;let mut, let &lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;const&lt;&#x2F;code&gt; 与 &lt;code&gt;let&lt;&#x2F;code&gt;无&lt;code&gt;mut&lt;&#x2F;code&gt;区别在于，&lt;code&gt;let&lt;&#x2F;code&gt;有&lt;code&gt;ownership&lt;&#x2F;code&gt;，而&lt;code&gt;const&lt;&#x2F;code&gt;随便使用&lt;&#x2F;p&gt;
&lt;h2 id=&quot;shu-ju-lei-xing&quot;&gt;数据类型&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;scalar&quot;&gt;&lt;code&gt;Scalar&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;integer, float, boolean, character&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;compound&quot;&gt;&lt;code&gt;Compound&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;tuple., array[]&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;han-shu&quot;&gt;函数&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; func_name&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;para&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; return_type&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;zhu-shi&quot;&gt;注释&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;kong-zhi-liu&quot;&gt;控制流&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;else if&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;loop&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;suo-you-quan&quot;&gt;所有权&lt;&#x2F;h2&gt;
&lt;p&gt;会转让或释放所有权的情况（储存堆上的变量）：
=移动、函数传参数、所在域结束
禁止发生的情况：
在当前域有两个只向同一变量的可变引用、
在当前域内同时有可变引用和不可变引用并且在可变引用后再次使用了之前的不可变引用、
代码块返回一个生命已经终结的引用、
可变引用会触发&lt;code&gt;borrowing&lt;&#x2F;code&gt;，引用指向的变量失去改变自己的权利，在引用死亡前禁止变量改变自己、
引用指向变量生命必须比引用本身长&lt;&#x2F;p&gt;
&lt;h2 id=&quot;jie-gou&quot;&gt;结构&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Struct_name&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    data_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;type_name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Struct_name&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;赋值};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Struct_name&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;struct1&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; build_struct&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;type_name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;note that all operation above takes the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;ownership from the parameters&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Point&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;实现结构内的方法：&lt;code&gt;impl,self&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;mei-ju&quot;&gt;枚举&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Kind&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    k1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    k2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;访问方式::&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;varient&lt;&#x2F;code&gt;可以附带有数据类型&lt;code&gt;()&lt;&#x2F;code&gt;互相可以不一样。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Message&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Quit&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;no data associated&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Move&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;},&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;has a name fields, like a struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    Write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; has a String&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    ChangeColor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; includes 3 value, like a tuple&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; enum_type&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    ::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; return_value&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; return_else_value&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F;for sequence only contains 2 branches&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; num_type_is_Option{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;not a num&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;match 必须包含所有branches&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;Rust 内置Option枚举&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    None&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;packages-crates-and-modules&quot;&gt;Packages, Crates and Modules&lt;&#x2F;h2&gt;
&lt;p&gt;调用&lt;code&gt;cargo new&lt;&#x2F;code&gt;会创建一个新项目，就是一个&lt;code&gt;Package&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Cargo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;toml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;src&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    main&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;rs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Crate&lt;&#x2F;code&gt;有两种类型：&lt;code&gt;bin&lt;&#x2F;code&gt;和&lt;code&gt;lib&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;rs &lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;bin类型的Crate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;rs &lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;lib类型的Crate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;一个package中的Crate可以有多个bin类型，最多只能有一个lib类型，&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;当有多个bin，需要放到src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;bin下。&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bin类型use的path不能使用crate开头，必须是Crate本名。&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;re&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;exporting&lt;&#x2F;span&gt;&lt;span&gt;： 利用pub修饰use，让导入当前scope的第三者可以引用当前scope已经use的item&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;module:&lt;&#x2F;code&gt;在Crate内组织代码&lt;&#x2F;p&gt;
&lt;h2 id=&quot;chang-yong-de-collections&quot;&gt;常用的&lt;code&gt;Collections&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;vectors&quot;&gt;&lt;code&gt;vectors&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;let v: Vec&amp;lt;i32&amp;gt; = Vec::new();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;mutable reference&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;return an Option&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;string-and-utf-8&quot;&gt;&lt;code&gt;String and UTF-8&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;use chars() or bytes() to visit item in side string&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;for i in &amp;quot;hello&amp;quot;.chars()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;chars&lt;&#x2F;span&gt;&lt;span&gt;(){&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;hashmap&quot;&gt;&lt;code&gt;HashMap&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;collections&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;HashMap&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; scores&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; HashMap&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;scores&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Blue&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;insert会夺走ownership&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; team_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Blue&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; score&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; score&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;team_name&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;copied&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap_or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;get()返回一个Option&amp;lt;&amp;amp;V&amp;gt;，copied返回Option&amp;lt;&amp;amp;V&amp;gt;的一个拷贝，得到&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;ownership，用unwrap_or()“解包”返回一个i32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;scores&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;overwriting&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; insert&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;adding&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; entry&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;entry()return &amp;amp;mut to the value and use or_insert() to insert&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;error-handling&quot;&gt;Error Handling&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;tong-cheng&quot;&gt;统称&lt;&#x2F;h2&gt;
&lt;p&gt;可用于&lt;code&gt;fn,struct,enum&lt;&#x2F;code&gt;
e.g.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; example&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;pa&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&amp;gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    do&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; stuff&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Y&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    gender&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    name&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; example&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; some_trait&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;pa&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;){&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;pass&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;te-dian&quot;&gt;特点&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;te-dian-de-sheng-ming&quot;&gt;特点的声明：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; summarize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;shi-yong-guan-jian-ci-forfu-yu-jie-gou-te-dian-li-zi-gei-liang-ge-bu-tong-de-jie-gou-ti-fu-yu-summaryde-te-dian&quot;&gt;使用关键词&lt;code&gt;for&lt;&#x2F;code&gt;赋予结构特点，例子给两个不同的结构体赋予&lt;code&gt;Summary&lt;&#x2F;code&gt;的特点：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; NewsArticle&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; headline&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; author&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; content&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; NewsArticle&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; summarize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;, by &lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;headline,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;author,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;location)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Tweet&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; username&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; content&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Tweet&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; summarize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        format!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;username,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self.&lt;&#x2F;span&gt;&lt;span&gt;content)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;diao-yong-fang-shi-lei-si-han-shu&quot;&gt;调用方式，类似函数：&lt;&#x2F;h3&gt;
&lt;p&gt;这里use还要包括&lt;code&gt;Summary&lt;&#x2F;code&gt;，因为&lt;code&gt;trait&lt;&#x2F;code&gt;是储存在块本地的；
另外，已经定义的结构和特点不能重复定义&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; aggregator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Summary&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Tweet&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; tweet&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Tweet&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        username&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;horse_ebooks&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        content&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;of course, as you probably already know, people&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        reply&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        retweet&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;1 new tweet: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; tweet&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;summarize&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;ke-yi-she-zhi-mo-ren-de-te-dian&quot;&gt;可以设置默认的特点：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; summarize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Read more...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; NewsArticle&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F;必须有这句话，而且括号内没有对应的方法&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;ba-te-dian-zuo-wei-han-shu-can-shu&quot;&gt;把特点作为函数参数：&lt;&#x2F;h3&gt;
&lt;p&gt;把特点作为参数的意义在于，任何实现了这个特点的数据类型都能作为这个函数的参数&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; notify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span&gt;){&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Breaking news! &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;summarize&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; notify&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;){&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;等价&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Breaking news! &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;summarize&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;tong-shi-man-zu-duo-ge-te-dian&quot;&gt;同时满足多个特点：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; notify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span&gt;)) {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; notify&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;){}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;geng-duo-li-zi&quot;&gt;更多例子：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; func&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; U&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Debug&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; u&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;U&lt;&#x2F;span&gt;&lt;span&gt;){}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; func&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;U&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; u&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;U&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    U&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Denug&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;yao-qiu-fan-hui-zhi-shi-xian-te-dian&quot;&gt;要求返回值实现特点：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; returns_summarizable&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Summary&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Article&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Jane&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to_string,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        published&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;shi-xian-bao-han-fan-xing-de-jie-gou-fang-fa-shi-qu-bie-dui-dai&quot;&gt;实现包含泛型的结构方法时，区别对待：&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pair&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pair&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;-&amp;gt;Self&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; ParialOrd&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Pari&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; cmp_display&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if self.&lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;gt;= self.&lt;&#x2F;span&gt;&lt;span&gt;y {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;lifetime&quot;&gt;&lt;code&gt;Lifetime&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;基本的在之前已经提到过&lt;&#x2F;p&gt;
&lt;h3 id=&quot;borrow-checker&quot;&gt;&lt;code&gt;Borrow Checker&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; r&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                &#x2F;&#x2F; ---------+-- &amp;#39;a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                     &#x2F;&#x2F;          |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; -+-- &amp;#39;b  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        r&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;           &#x2F;&#x2F;  |       |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                     &#x2F;&#x2F; -+       |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;r: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; r&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F;          |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                         &#x2F;&#x2F; ---------+&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;            &#x2F;&#x2F; ----------+-- &amp;#39;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; r&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;           &#x2F;&#x2F; --+-- &amp;#39;a  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;r: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; r&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt; &#x2F;&#x2F; --+       |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;                         &#x2F;&#x2F; ----------+&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;can-shu-wei-yin-yong-bian-yi-qi-wu-fa-pan-duan-sheng-ming-zhou-qi-shi&quot;&gt;参数为引用编译器无法判断生命周期时&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;() &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        y&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; 参数和返回值具有相同的生命周期&amp;#39;a &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; 返回值的lifetime = min(x,y)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;之所以参数都要需要标记，是因为&lt;code&gt;x&lt;&#x2F;code&gt;，&lt;code&gt;y&lt;&#x2F;code&gt;都有可能被返回&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;jie-gou-ti-nei-de-bian-liang-ye-ke-yi-shi-yin-yong-dan-shi-bi-xu-zhi-ming-sheng-ming-zhou-qi&quot;&gt;结构体内的变量也可以是引用，但是必须指明生命周期&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; SSS&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    a_str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;意味着，一个SSS实例的lifetime不能大于a_str&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;(){&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sss&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; SSS&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        a_str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; tmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;生命周期过短&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            tmp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F;a_str:&amp;quot;hello&amp;quot;,&#x2F;&#x2F;正常运行&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;sss&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;a_str);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;wu-xu-sheng-ming-sheng-ming-zhou-qi-de-qing-kuang-lifetime-elision-rules&quot;&gt;无需声明生命周期的情况(&lt;code&gt;lifetime elision rules&lt;&#x2F;code&gt;)&lt;&#x2F;h3&gt;
&lt;p&gt;编译器检查生命周期的三条规则，一旦能确认所有参数和输出值的生命周期，编译器不强制要求添加&lt;code&gt;lifetime annotation&lt;&#x2F;code&gt;：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;给每个参数赋予不同的生命周期&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;只有一个参数时，返回值的生命周期和这个参数一样&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;当参数形式为&lt;code&gt;&amp;amp;self&lt;&#x2F;code&gt;时，因为&lt;code&gt;self&lt;&#x2F;code&gt;，所以所有生命周期都是&lt;code&gt;self&lt;&#x2F;code&gt;的生命周期&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; first_word&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;rule 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; first_word&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;rule 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; first_word&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;rule 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;rule1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;b str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;rule2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;fan-xing-te-dian-sheng-ming-zhou-qi-da-za-hui&quot;&gt;泛型、特点、生命周期大杂烩&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Display&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; longest_with_an_announcement&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;   x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    ann&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a str&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Display&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Announcement! &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; ann&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;() &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        y&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;ce-shi&quot;&gt;测试&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;san-ge-ce-shi-han-shu&quot;&gt;三个测试函数&lt;&#x2F;h3&gt;
&lt;p&gt;在&lt;code&gt;Rust&lt;&#x2F;code&gt;中，可以为自己编写的程序编写测试以测试代码的正确性。
在终端调用&lt;code&gt;cargo new proj_name --lib&lt;&#x2F;code&gt;会自动生成项目，&lt;code&gt;src&lt;&#x2F;code&gt;文件夹的&lt;code&gt;lib.rs&lt;&#x2F;code&gt;会自带&lt;code&gt;test module&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[cfg(test)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mod&lt;&#x2F;span&gt;&lt;span&gt; tests {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; it_works&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;result&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;其中&lt;code&gt;#[tests]&lt;&#x2F;code&gt;用来指示接下来的函数是用于测试的函数，&lt;code&gt;assert_eq!()&lt;&#x2F;code&gt;判断参数是否相同，当结果不同或者代码发生&lt;code&gt;panic!&lt;&#x2F;code&gt;就会测试失败，另&lt;code&gt;assert_ne!()&lt;&#x2F;code&gt;则判断是否不同。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;assert!&lt;&#x2F;code&gt;参数类型是&lt;code&gt;Boolean&lt;&#x2F;code&gt;，如果参数为&lt;code&gt;false&lt;&#x2F;code&gt;则测试失败。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;partialeq-debug-te-dian&quot;&gt;PartialEq Debug 特点&lt;&#x2F;h3&gt;
&lt;p&gt;如果一个结构体或者枚举想要作为参数传入上述函数，必须实现&lt;code&gt;PartialEq&lt;&#x2F;code&gt;和&lt;code&gt;Debug&lt;&#x2F;code&gt;这两个特点，（前者在于&lt;code&gt;==、!=&lt;&#x2F;code&gt;的比较，后者在于代码判断为&lt;code&gt;Failed&lt;&#x2F;code&gt;时要打印错误信息）&lt;&#x2F;p&gt;
&lt;h3 id=&quot;zi-ding-yi-shi-bai-xin-xi&quot;&gt;自定义失败信息&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;assert&lt;&#x2F;code&gt;函数第二个参数即为自定义字符串信息：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; greeting&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Hello!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[cfg(test)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mod&lt;&#x2F;span&gt;&lt;span&gt; tests {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    use super::*&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; greeting_contains_name&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; greeting&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Carol&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;contains&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Carol&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Greeting did not contain name, value was `&lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            result&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;报错信息则为&lt;code&gt;&quot;Greeting did not contain name, value was &#x27;Carol&#x27;&quot;&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;should-panic&quot;&gt;should_panic&lt;&#x2F;h3&gt;
&lt;p&gt;在&lt;code&gt;#[test]&lt;&#x2F;code&gt;属性下一行添加&lt;code&gt;#[should_panic]&lt;&#x2F;code&gt;，让编译器认为检测函数应该&lt;code&gt;panic&lt;&#x2F;code&gt;，否则测试不通过。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[should_panic]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; example&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    panic!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;This is supposed to panic.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;测试ok&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;使用&lt;code&gt;expected&lt;&#x2F;code&gt;可以使&lt;code&gt;should_panic&lt;&#x2F;code&gt;更加精确：
例如&lt;code&gt;#[should_panic(expected = &quot;is supposed&quot;)]&lt;&#x2F;code&gt;替换上面的&lt;code&gt;#[should_panic]&lt;&#x2F;code&gt;，
则测试可以通过，因为&lt;code&gt;panic contained expected string&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;result-t-e&quot;&gt;Result&amp;lt;T, E&amp;gt;&lt;&#x2F;h3&gt;
&lt;p&gt;在&lt;code&gt;test module&lt;&#x2F;code&gt;中，可以使用&lt;code&gt;Result&lt;&#x2F;code&gt;枚举来代替&lt;code&gt;assert&lt;&#x2F;code&gt;系列函数：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[cfg(test)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mod&lt;&#x2F;span&gt;&lt;span&gt; tests {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; it_works&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span&gt;(())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;two plus two does not equal four&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;值得注意的是，在这种情况下不能使用&lt;code&gt;should_panic&lt;&#x2F;code&gt;，非要使用的话，可以在函数最后&lt;code&gt;assert!(value.is_err())&lt;&#x2F;code&gt;;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;kong-zhi-ce-shi-fang-shi&quot;&gt;控制测试方式&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;cargo test&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;-- --test-thread=1 &lt;&#x2F;code&gt;线程设置为&lt;code&gt;1&lt;&#x2F;code&gt;，禁止测试函数并行运行&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-- --show-output &lt;&#x2F;code&gt;就算测试不通过也打印过程中&lt;code&gt;println&lt;&#x2F;code&gt;的信息&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;cargo test prefix*&lt;&#x2F;code&gt; 只测试以&lt;code&gt;prefix&lt;&#x2F;code&gt;打头的函数&lt;&#x2F;li&gt;
&lt;li&gt;在代码中&lt;code&gt;#[test]&lt;&#x2F;code&gt;下一行添加&lt;code&gt;#[ignore]&lt;&#x2F;code&gt;可以使测试忽略当前函数&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-- --ignored&lt;&#x2F;code&gt; 只运行标记了&lt;code&gt;ignore&lt;&#x2F;code&gt;的函数&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;dan-yuan-ce-shi-he-ji-cheng-ce-shi&quot;&gt;单元测试和集成测试&lt;&#x2F;h3&gt;
&lt;p&gt;单元测试：
更小更集中，在隔离的环境中测试一个模块，上面的例子都是单元测试；
使用&lt;code&gt;#[cfg(test)]&lt;&#x2F;code&gt;标注，只在&lt;code&gt;cargo test&lt;&#x2F;code&gt;时编译和运行&lt;&#x2F;p&gt;
&lt;p&gt;集成测试：
在外部的，位于与一般代码相同的文件中。需要新建与&lt;code&gt;src&lt;&#x2F;code&gt;同级的&lt;code&gt;tests&lt;&#x2F;code&gt;目录，在目录下新建&lt;code&gt;test_name.rs&lt;&#x2F;code&gt;，需要使用&lt;code&gt;use&lt;&#x2F;code&gt;导入测试目标&lt;code&gt;crate&lt;&#x2F;code&gt;，代码无需&lt;code&gt;cfg&lt;&#x2F;code&gt;标记，只需在每个测试函数前&lt;code&gt;#[test]&lt;&#x2F;code&gt;即可。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;bi-bao&quot;&gt;闭包&lt;&#x2F;h2&gt;
&lt;p&gt;任何花括号包括的区域最后一条语句没有加分号都有返回值，{}闭包就是可以保存进变量 或 作为参数传递给其他函数 的 匿名函数。
函数变量e.g.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;  add_one_v1&lt;&#x2F;span&gt;&lt;span&gt;   (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; add_one_v2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;| -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt; };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; add_one_v3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;             {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt; };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; add_one_v4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;               x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;  ;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;die-dai-qi&quot;&gt;迭代器&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;iterator-next&quot;&gt;Iterator &amp;amp; next&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;  type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Item&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;  fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;如果直接把迭代器 &lt;code&gt;iter()&lt;&#x2F;code&gt; 赋值给变量 &lt;code&gt;v1&lt;&#x2F;code&gt;，那么在直接调用 &lt;code&gt;v1.next()&lt;&#x2F;code&gt; 要求 &lt;code&gt;v1 &lt;&#x2F;code&gt;是可变的，如果是 &lt;code&gt;for&lt;&#x2F;code&gt; 来遍历则不需要，因为 &lt;code&gt;rust &lt;&#x2F;code&gt;后台自动获取 &lt;code&gt;v1&lt;&#x2F;code&gt; 的所有权并使它可变。&lt;&#x2F;p&gt;
&lt;p&gt;需要注意，&lt;code&gt;vector  &lt;&#x2F;code&gt;迭代器的 &lt;code&gt;next &lt;&#x2F;code&gt; 返回的是不可变引用，如果需要得到所有权，可以调用&lt;code&gt;into_iter&lt;&#x2F;code&gt;；如果需要可变迭代引用，可以调用 &lt;code&gt;iter_mut&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;xiao-fei-die-dai-qi-de-fang-fa&quot;&gt;消费迭代器的方法&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Iterator trait &lt;&#x2F;code&gt;中调用了 &lt;code&gt;next &lt;&#x2F;code&gt;的方法被称为&lt;strong&gt;消费适配器&lt;&#x2F;strong&gt;( &lt;code&gt;consuming adaptors&lt;&#x2F;code&gt; )，这些方法会消耗迭代器，夺走所有权。&lt;&#x2F;p&gt;
&lt;p&gt;以sum为例：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; iterator_sum&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v1_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; total&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v1_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;total&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这里调用 &lt;code&gt;sum&lt;&#x2F;code&gt; 之后不再允许使用 &lt;code&gt;v1_iter&lt;&#x2F;code&gt;，因为所有权用完了。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;jian-dan-bian-xie-zi-ding-yi-next&quot;&gt;简单编写自定义next&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;  	count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; 		fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    		cnt&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  	}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; cnt&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;  	type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; u32&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;  	fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;      	self.&lt;&#x2F;span&gt;&lt;span&gt;count &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    		if self.&lt;&#x2F;span&gt;&lt;span&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;      			Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;self.&lt;&#x2F;span&gt;&lt;span&gt;count)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  			}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;      			None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      	}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  	}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这样，就可以获得可以&lt;code&gt;next&lt;&#x2F;code&gt;六次的迭代器了。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;xun-huan-vsdie-dai-qi&quot;&gt;循环VS迭代器&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; coefficients&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i64&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; qlp_shift&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i16&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;      let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; prediction&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; coefficients&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                                 .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;zip&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                                 .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i64&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;                                 .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; qlp_shift&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; buffer&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    buffer&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; prediction&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; i32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; delta&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;为了计算 &lt;code&gt;prediction&lt;&#x2F;code&gt; 的值，这些代码遍历了 &lt;code&gt;coefficients&lt;&#x2F;code&gt; 中的 12 个值，使用 &lt;code&gt;zip&lt;&#x2F;code&gt; 方法将系数与 &lt;code&gt;buffer&lt;&#x2F;code&gt; 的前 12 个值组合在一起。接着将每一对值相乘，再将所有结果相加，然后将总和右移 &lt;code&gt;qlp_shift&lt;&#x2F;code&gt; 位。&lt;&#x2F;p&gt;
&lt;p&gt;这里，我们创建了一个迭代器，使用了两个适配器，接着消费了其值。Rust 代码将会被编译为什么样的汇编代码呢？遍历 &lt;code&gt;coefficients&lt;&#x2F;code&gt; 的值完全用不到循环：Rust 知道这里会迭代 12 次，所以它“展开”（unroll）了循环。展开是一种移除循环控制代码的开销并替换为每个迭代中的重复代码的优化。&lt;&#x2F;p&gt;
&lt;p&gt;所有的系数都被储存在了寄存器中，这意味着访问他们非常快。这里也没有运行时数组访问边界检查。所有这些 Rust 能够提供的优化使得结果代码极为高效。&lt;&#x2F;p&gt;
&lt;h2 id=&quot;zhi-neng-zhi-zhen&quot;&gt;智能指针&lt;&#x2F;h2&gt;
&lt;p&gt;其实，应用最多的指针是 &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; 引用，但是Rust中有一类结构实现了 &lt;code&gt;Deref&lt;&#x2F;code&gt; 和 &lt;code&gt;Drop&lt;&#x2F;code&gt; trait，前者让结构体实例能像引用那样（可以“解包”），后者可以自定义当智能指针离开作用域要运行的代码。&lt;&#x2F;p&gt;
&lt;p&gt;其实，&lt;code&gt;String  &lt;&#x2F;code&gt; 和 &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 都属于智能指针。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;box-t&quot;&gt;&lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;在堆上储存数据，在栈上储存指针。&lt;&#x2F;p&gt;
&lt;h4 id=&quot;di-gui-mei-ju&quot;&gt;递归枚举&lt;&#x2F;h4&gt;
&lt;p&gt;如果要自定义一个枚举列表：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Nil&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;上面的代码是不能编译的，因为List中永远有下一个List，而每个List都附带一个i32（在栈上），编译器不知道具体要分配多少空间。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;trpl15-01.svg&quot; alt=&quot;An infinite Cons list&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Nil&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use crate::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Nil&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; list&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;                Box&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Nil&lt;&#x2F;span&gt;&lt;span&gt;))))));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;上面的代码就是可行的&lt;img src=&quot;&#x2F;images&#x2F;trpl15-02.svg&quot; alt=&quot;A finite Cons list&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;zi-ding-yi-yi-ge-zhi-neng-zhi-zhen&quot;&gt;自定义一个智能指针&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;shi-xian-deref-trait&quot;&gt;实现Deref trait&lt;&#x2F;h4&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;ops&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Deref&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;  	fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;      	MyBox&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Deref&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; deref&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        &amp;amp;self.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;以上代码能够让 &lt;code&gt;MyBox&lt;&#x2F;code&gt; 能够像 &lt;code&gt;dereference&lt;&#x2F;code&gt; 引用 &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; 一样解包 &lt;code&gt;MyBox&lt;&#x2F;code&gt; 实例，这里返回的是 &lt;code&gt;&amp;amp;x&lt;&#x2F;code&gt;，因此实际Rust在底层运行的代码是：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;deref&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;y是一个MyBox实例，deref()返回的是引用&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;jie-zhu-deref-traitshi-xian-yin-shi-jie-yin-yong-qiang-zhi-zhuan-huan&quot;&gt;借助Deref trait实现隐式解引用强制转换&lt;&#x2F;h3&gt;
&lt;p&gt;有这样一个函数：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; hello&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;要求参数是 &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;，而：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Rust&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    hello&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;这段代码却可以正常运行，这是因为 &lt;code&gt;MyBox&lt;&#x2F;code&gt; 和 &lt;code&gt;String&lt;&#x2F;code&gt; 都有实现 &lt;code&gt;Deref&lt;&#x2F;code&gt; trait，这里使用 &lt;code&gt;&amp;amp;m&lt;&#x2F;code&gt; 调用 &lt;code&gt;hello&lt;&#x2F;code&gt; 函数，其为 &lt;code&gt;MyBox&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; 值的引用。 Rust 可以通过 &lt;code&gt;deref&lt;&#x2F;code&gt; 调用将 &lt;code&gt;&amp;amp;MyBox&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; 变为 &lt;code&gt;&amp;amp;String&lt;&#x2F;code&gt;。Rust 再次调用 &lt;code&gt;deref&lt;&#x2F;code&gt; 将 &lt;code&gt;&amp;amp;String&lt;&#x2F;code&gt; 变为 &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;，这就符合 &lt;code&gt;hello&lt;&#x2F;code&gt; 函数的定义了。&lt;&#x2F;p&gt;
&lt;p&gt;如果没有实现解引用强制转换，代码将必须写成这样：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MyBox&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Rust&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    hello&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;以上解析都是发生在编译时，不会有运行损耗。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;drop-trait&quot;&gt;Drop Trait&lt;&#x2F;h3&gt;
&lt;p&gt;之所以把 &lt;code&gt;Drop&lt;&#x2F;code&gt; trait 放在智能指针这里，是因为 &lt;code&gt;Drop&lt;&#x2F;code&gt; 几乎都是用于实现智能指针。&lt;&#x2F;p&gt;
&lt;p&gt;可以直接把 &lt;code&gt;Drop&lt;&#x2F;code&gt; 里的 &lt;code&gt;drop()&lt;&#x2F;code&gt; 理解为析构函数，我们不能直接调用 &lt;code&gt;drop&lt;&#x2F;code&gt; 因为这样的话作用域结束后又会调用一次，如果需要提前释放变量可以使用 &lt;code&gt;std::mem::drop&lt;&#x2F;code&gt; 这个函数不同于前者，位于 prelude 中，通过调用它可以提前释放变量。无需担心之后发生“空指针”的问题，因为编译起会帮你检测，确保引用总是有效的。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;rc-t-yin-yong-ji-shu-zhi-neng-zhi-zhen&quot;&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 引用计数智能指针&lt;&#x2F;h3&gt;
&lt;p&gt;Rust 内置一个叫做 &lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 的类型，是 &lt;code&gt;reference counting&lt;&#x2F;code&gt; 的缩写。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 用于当我们希望在堆上分配内存给程序多个部分读取，而且无法在编译时确定程序的哪一部分会最后结束使用它的时候。如果确实知道哪一部分是最后结束使用的话，就能令其成为数据的所有者，正常的所有权规则就可以在编译时运行。&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;RC&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 只能用于单线程场景。&lt;&#x2F;p&gt;
&lt;p&gt;类似浅拷贝，不会完整拷贝 &lt;code&gt;T&lt;&#x2F;code&gt; 所有数据，而是让&lt;strong&gt;引用计数&lt;&#x2F;strong&gt;增加。在场景：多个列表需要共享一段列表时，不会有所有权“不够用”或者深拷贝低效率的问题。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Nil&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use crate::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Nil&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Nil&lt;&#x2F;span&gt;&lt;span&gt;)))));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;count after creating a = &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;strong_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;count after creating b = &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;strong_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;count after creating c = &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;strong_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;count after c goes out of scope = &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;strong_count&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;OUTPUT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;count after creating a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;count after creating b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;count after creating c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;count after c goes out of scope&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许程序多个部分只读共享数据。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;refcell-t-he-nei-bu-ke-bian-xing-mo-shi&quot;&gt;&lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 和内部可变性模式&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;内部可变性&lt;&#x2F;strong&gt;（&lt;em&gt;Interior mutability&lt;&#x2F;em&gt;）是 Rust 中的一个设计模式，允许你在有不可变量引用存在的情况下也能改变数据。使用 &lt;code&gt;unsafe&lt;&#x2F;code&gt; 来模糊 Rust 通常的可变性和借用规则。&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 代表其数据唯一所有权。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 用于当你确定代码是正确的，但是编译器不这么认为的时候。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 同样只能用于单线程场景。&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;如下为选择 &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;，&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 或 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 的理由：&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许相同数据有多个所有者；&lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 和 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 有单一所有者。&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Box&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许在编译时执行不可变或可变借用检查；&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;仅允许在编译时执行不可变借用检查；&lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许在运行时执行不可变或可变借用检查。&lt;&#x2F;li&gt;
&lt;li&gt;因为 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许在运行时执行可变借用检查，所以我们可以在即便 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 自身是不可变的情况下修改其内部的值。&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;假如现在有一个结构 &lt;code&gt;A&lt;&#x2F;code&gt; ，结构 &lt;code&gt;B&lt;&#x2F;code&gt; 内的变量有 &lt;code&gt;&amp;amp;A&lt;&#x2F;code&gt; ，一般来说，我们是不能改变 &lt;code&gt;B&lt;&#x2F;code&gt; 的，因为它不是可变引用，但是如果 &lt;code&gt;B&lt;&#x2F;code&gt; 里面的某个变量是 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; ，那么就可以调用 &lt;code&gt;B.some_variable.borrow_mut()&lt;&#x2F;code&gt; ，这会返回一个可变引用在&lt;strong&gt;内部&lt;&#x2F;strong&gt;实现数据更改。&lt;&#x2F;p&gt;
&lt;p&gt;实例代码：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Messenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; msg&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LimitTracker&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Messenger&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LimitTracker&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    where&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Messenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LimitTracker&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;        LimitTracker&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            messenger&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            max&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; set_value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; usize&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        self.&lt;&#x2F;span&gt;&lt;span&gt;value &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; percentage_of_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; = self.&lt;&#x2F;span&gt;&lt;span&gt;value &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; f64&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &#x2F; self.&lt;&#x2F;span&gt;&lt;span&gt;max &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; f64&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; percentage_of_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Error: You are over your quota!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; percentage_of_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;             self.&lt;&#x2F;span&gt;&lt;span&gt;messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Urgent warning: You&amp;#39;ve used up over 90% of your quota!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; else if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; percentage_of_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;75&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Warning: You&amp;#39;ve used up over 75% of your quota!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[cfg(test)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;mod&lt;&#x2F;span&gt;&lt;span&gt; tests {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    use super::*&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MockMessenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        sent_messages&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RefCell&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MockMessenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MockMessenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;            MockMessenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; sent_messages&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; RefCell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[]) }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MockMessenger&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; message&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            self.&lt;&#x2F;span&gt;&lt;span&gt;sent_messages&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    #[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; it_sends_an_over_75_percent_warning_message&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;        &#x2F;&#x2F; --snip--&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; mock_messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; MockMessenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; limit_tracker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; LimitTracker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;mock_messenger&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        limit_tracker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;set_value&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;75&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;mock_messenger&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;sent_messages&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;borrow&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h4 id=&quot;yuan-li&quot;&gt;原理&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;code&gt;borrow&lt;&#x2F;code&gt; 返回 &lt;code&gt;Ref&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;，而 &lt;code&gt;borrow_mut&lt;&#x2F;code&gt; 则返回 &lt;code&gt;RefMut&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 二者都是智能指针。类似的，&lt;code&gt;RefCell&lt;&#x2F;code&gt; 只允许有多个不可变借用或一个可变借用。如果违反规则的话，相比编译时出错， &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 会在运行时 panic。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;rc-t-he-refcell-t-da-pei-shi-yong&quot;&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 和 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;搭配使用&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 允许统一数据有多个所有者但不可变引用&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;RefCell&lt;&#x2F;code&gt; 可以返回可变引用修改不可变引用的值&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;由此，如果 &lt;code&gt;Rc&amp;lt;RefCell&amp;lt;T&amp;gt;&amp;gt;&lt;&#x2F;code&gt; 这样存放数据，就可以得到多个所有者&lt;strong&gt;并且&lt;&#x2F;strong&gt;可以修改的值了。&lt;&#x2F;p&gt;
&lt;p&gt;例子：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[derive(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Debug&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;    Nil&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use crate::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Nil&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;cell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Nil&lt;&#x2F;span&gt;&lt;span&gt;)));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; Cons&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;RefCell&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F; 这里*解引用得到 RefCell，然后对其调用.borrow_mut&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a after = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;b after = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;c after = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;li-yong-thread-spawn-shi-xian-duo-xian-cheng&quot;&gt;利用 &lt;code&gt;thread::spawn&lt;&#x2F;code&gt; 实现多线程&lt;&#x2F;h3&gt;
&lt;p&gt;结合闭包和 &lt;code&gt;spawn&lt;&#x2F;code&gt; 函数产生子线程：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;thread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;转移所有权&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Here&amp;#39;s a vector: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; v&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Here&amp;#39;s a character: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    print!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Here&amp;#39;s a character: &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;    &#x2F;&#x2F;println!(&amp;quot;Here&amp;#39;s a vector: {:?}&amp;quot;, v);会报错，无所有权&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;    handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;等待子线程结束&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;li-yong-mpsc-channel-chuang-jian-tong-dao-yong-yu-jin-cheng-jian-tong-xun&quot;&gt;利用 &lt;code&gt;mpsc::channel&lt;&#x2F;code&gt; 创建通道用于进程间通讯&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;mpsc&lt;&#x2F;code&gt; 的含义是 &lt;em&gt;mutiple producer, single consumer&lt;&#x2F;em&gt;。&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;thread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;mpsc;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;tx&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; rx&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; mpsc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;channel&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hi&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        tx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; received&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; rx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;recv&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;  &#x2F;&#x2F;rx.try_recv()尝试接收但是不阻塞&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Got: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; received&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;使用 &lt;code&gt;send&lt;&#x2F;code&gt; 发送讯息，同样送走了变量的所有权（堆）。&lt;&#x2F;p&gt;
&lt;p&gt;可以将 &lt;code&gt;rx&lt;&#x2F;code&gt; 作为一个迭代器使用：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;thread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;mpsc;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;tx&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; rx&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; mpsc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;channel&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; vals&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;hi&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;the&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            String&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;thread&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; vals&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;            tx&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;from_secs&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; received&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; rx&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;这里！阻塞地等待&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;        println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Got: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; received&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;如果要实现多个生产者端口，可以使用 &lt;code&gt;clone&lt;&#x2F;code&gt; 得到多个 &lt;code&gt;tx&lt;&#x2F;code&gt;。&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mutex-t-hu-chi-qi&quot;&gt;&lt;code&gt;Mutex&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 互斥器&lt;&#x2F;h3&gt;
&lt;p&gt;单线程 &lt;code&gt;Mutex&lt;&#x2F;code&gt; 的一个简单实例：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;创建Mutex&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;返回Mutex内的可变引用&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;        *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;m = &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;:?&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F;将发现m内为6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;如果多个线程都要使用一个 &lt;code&gt;Mutex&lt;&#x2F;code&gt; ，那就不好搞啦，因为 &lt;code&gt;spawn&lt;&#x2F;code&gt; 创建的子线程如若要调用 &lt;code&gt;Mutex&lt;&#x2F;code&gt; ，则 &lt;code&gt;Mutex&lt;&#x2F;code&gt; 所有权进入子线程，后面创建的子线程就不能用了，如下：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;thread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handles&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        handles&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handles&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Result: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5F6672;font-style: italic;&quot;&gt;&#x2F;&#x2F; 这段代码无法编译通过&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;但是我们也不能用之前提到过的智能指针 &lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 实现多引用，我们需要一个和它类似的类型。&lt;&#x2F;p&gt;
&lt;p&gt;从例子也可以看出， &lt;code&gt;Mutex&lt;&#x2F;code&gt; 也是具有内部可变性的。&lt;&#x2F;p&gt;
&lt;h4 id=&quot;arc-t-yuan-zi-yin-yong-ji-shu&quot;&gt;&lt;code&gt;Arc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; 原子引用计数&lt;&#x2F;h4&gt;
&lt;p&gt;使用例：&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #A9B2C3; background-color: #21252B;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;sync&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;thread;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handles&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt; vec!&lt;&#x2F;span&gt;&lt;span&gt;[];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E5C07B;&quot;&gt; Arc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;counter&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt; thread&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;move ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #61AFEF;&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;            *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #56B6C2;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        handles&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt; handles&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;        handle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;    println!&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #98C379;&quot;&gt;Result: &lt;&#x2F;span&gt;&lt;span&gt;{}&amp;quot;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C6CCD7;&quot;&gt;counter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E06C75;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B57EDC;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
</feed>
