<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex</title>
    <description>The latest articles on DEV Community by Alex (@zhanghandong).</description>
    <link>https://dev.to/zhanghandong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F42178%2F0796c61b-6fa3-4a3f-a298-a9c708eedcc2.jpeg</url>
      <title>DEV Community: Alex</title>
      <link>https://dev.to/zhanghandong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhanghandong"/>
    <language>en</language>
    <item>
      <title>Is Unsafe the Original Sin? A Deep Dive into the First CVE After Rust Entered the Linux Kernel</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Thu, 18 Dec 2025 14:10:00 +0000</pubDate>
      <link>https://dev.to/zhanghandong/is-unsafe-the-original-sin-a-deep-dive-into-the-first-cve-after-rust-entered-the-linux-kernel-39k</link>
      <guid>https://dev.to/zhanghandong/is-unsafe-the-original-sin-a-deep-dive-into-the-first-cve-after-rust-entered-the-linux-kernel-39k</guid>
      <description>&lt;h2&gt;
  
  
  Prelude: A Predictable Controversy
&lt;/h2&gt;

&lt;p&gt;On December 16, 2025, a peculiar entry appeared in the Linux kernel CVE announcement list: &lt;a href="https://nvd.nist.gov/vuln/detail/CVE-2025-68260" rel="noopener noreferrer"&gt;CVE-2025-68260&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This was no ordinary kernel vulnerability. It came from &lt;code&gt;rust_binder&lt;/code&gt;, the Rust implementation of the Android Binder driver in the Linux kernel. This marks &lt;strong&gt;the first CVE officially assigned to Rust code&lt;/strong&gt; since Rust entered the Linux kernel mainline.&lt;/p&gt;

&lt;p&gt;The news set social media ablaze.&lt;/p&gt;

&lt;p&gt;Rust critics felt they had finally found the "smoking gun" they'd been waiting for:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Since unsafe is unavoidable in the kernel, and unsafe code has hidden dangers, maybe you shouldn't have been bragging everywhere that 'if it compiles, it works.' So now we need to add another condition that unsafe code doesn't count as compiled code?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust supporters quickly fired back:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Before you start trending on the first reported CVE linked to rust code on the linux kernel, note that: &lt;strong&gt;on the same day, 159 kernel CVEs were issued for the C portion of the codebase&lt;/strong&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One C developer even stepped up to defend Rust:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"It's funny how as a C dev I gotta defend Rust, but there are over a hundred of these daily caused by C code. A single non-critical CVE in 5 years doesn't seem that big with this information."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The debate ultimately centered on a frequently misunderstood feature of the Rust language: &lt;strong&gt;Unsafe&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So today, let's use this CVE as an opportunity to thoroughly understand: What exactly is Unsafe Rust? Is it a "loophole" in Rust's safety promises, or a "necessary evil" of systems programming?&lt;/p&gt;




&lt;h2&gt;
  
  
  Part One: What Exactly Happened with This CVE?
&lt;/h2&gt;

&lt;p&gt;Before diving into unsafe, let's first understand the vulnerability itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Background: Safe Abstraction in Unsafe Rust
&lt;/h3&gt;

&lt;p&gt;To understand this vulnerability, we first need to understand one of Rust's core design principles: &lt;strong&gt;Safe Abstraction&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rust divides code into two worlds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Safe Rust&lt;/strong&gt;: The compiler &lt;strong&gt;automatically proves&lt;/strong&gt; memory safety at compile time through mechanisms like ownership and the borrow checker. You don't need to do anything extra—if it compiles, it means there are no data races, dangling pointers, buffer overflows, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unsafe Rust&lt;/strong&gt;: Certain low-level operations (like dereferencing raw pointers or calling FFI functions) exceed the compiler's static analysis capabilities. In these cases, developers must mark code blocks with the &lt;code&gt;unsafe&lt;/code&gt; keyword, &lt;strong&gt;personally promising the compiler&lt;/strong&gt;: I have manually verified that this code is safe.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key point is: code inside an &lt;code&gt;unsafe&lt;/code&gt; block still receives the same memory safety checks as Safe Rust (Unsafe is a superset of Safe), but for certain specific operations—like manipulating raw pointers—the compiler simply cannot help, so developers must guarantee safety themselves.&lt;/p&gt;

&lt;p&gt;This raises a question: &lt;strong&gt;How do you prove your promise is trustworthy?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;SAFETY comments&lt;/strong&gt;: a community-established documentation convention. Above every unsafe block, developers must clearly state:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Why is unsafe needed here?&lt;/strong&gt; (What operation can't the compiler verify?)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What preconditions does safety depend on?&lt;/strong&gt; (i.e., "invariants")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why do these preconditions hold?&lt;/strong&gt; (Provide the reasoning)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This isn't optional "nice-to-have" documentation—it's a &lt;strong&gt;mandatory convention&lt;/strong&gt; in the Rust community. Unsafe code without SAFETY comments will be rejected outright in code review.&lt;/p&gt;

&lt;p&gt;Why? Because the correctness of unsafe code &lt;strong&gt;depends entirely on whether these invariants hold&lt;/strong&gt;. If invariants are wrong, incomplete, or become invalid as code evolves, bugs will emerge.&lt;/p&gt;

&lt;p&gt;Now, let's look at the problematic code in this CVE.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problematic Code
&lt;/h3&gt;

&lt;p&gt;The vulnerability appeared in the &lt;code&gt;drivers/android/binder/node.rs&lt;/code&gt; file. To understand this bug, we need some background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Binder?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Binder is the core Inter-Process Communication (IPC) mechanism in Android. When one app wants to call another app or system service, it goes through Binder. It's one of the cornerstones that makes Android work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is death_list?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Binder, there's an important concept called "Death Notification." When a service process crashes unexpectedly, all clients depending on it need to be notified. The &lt;code&gt;NodeDeath&lt;/code&gt; struct represents such death notifications, and &lt;code&gt;death_list&lt;/code&gt; is a linked list storing all death notifications registered to a particular Binder node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is this code doing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When cleaning up a &lt;code&gt;NodeDeath&lt;/code&gt;, the code needs to remove it from its associated &lt;code&gt;death_list&lt;/code&gt; linked list. The problem lies in this removal operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SAFETY: A `NodeDeath` is never inserted into the death_list&lt;/span&gt;
&lt;span class="c1"&gt;// of any node other than its owner, so it is either in this&lt;/span&gt;
&lt;span class="c1"&gt;// death list or in no death list.&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;node_inner&lt;/span&gt;&lt;span class="py"&gt;.death_list&lt;/span&gt;&lt;span class="nf"&gt;.remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The SAFETY comment claims: &lt;strong&gt;A &lt;code&gt;NodeDeath&lt;/code&gt; node is only ever inserted into the death_list of its owning node, so it's either in this list or in no list at all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on this assumption, the developer believed this unsafe operation was safe.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But the problem is: this assumption doesn't hold in concurrent scenarios.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Formation of the Race Condition
&lt;/h3&gt;

&lt;p&gt;Let's look at the logic of the &lt;code&gt;Node::release&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thread A (Node::release):
  1. Acquire lock
  2. Move all items from death_list to a temporary list on the stack
  3. Release lock  ← Problem is here
  4. Iterate through the temporary list, processing each node

Thread B (NodeDeath cleanup):
  At some point calls unsafe { death_list.remove(self) }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the problem?&lt;/p&gt;

&lt;p&gt;After Thread A releases the lock in step 3 but before completing step 4, Thread B might simultaneously call &lt;code&gt;remove&lt;/code&gt; on the original list. At this point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thread A is traversing via the temporary list, accessing nodes' &lt;code&gt;prev/next&lt;/code&gt; pointers&lt;/li&gt;
&lt;li&gt;Thread B is removing via the original list, also modifying nodes' &lt;code&gt;prev/next&lt;/code&gt; pointers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two threads modifying the same memory simultaneously, with no synchronization mechanism&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://pbs.twimg.com/media/G8YfVRnW4AEALhU?format=jpg&amp;amp;name=4096x4096" rel="noopener noreferrer"&gt;https://pbs.twimg.com/media/G8YfVRnW4AEALhU?format=jpg&amp;amp;name=4096x4096&lt;/a&gt;&lt;br&gt;
(Diagram from &lt;a href="//x.com/forefy"&gt;x.com/forefy&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;This is a classic &lt;strong&gt;Data Race&lt;/strong&gt;, corrupting the linked list pointers. The end result is a kernel crash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unable to handle kernel paging request at virtual address 000bb9841bcac70e
...
Internal error: Oops: 0000000096000044 [#1] PREEMPT SMP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Fix
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The fix is actually simple&lt;/strong&gt;: instead of moving items to a temporary list, &lt;strong&gt;pop and process items directly from the original list while holding the lock&lt;/strong&gt;. This eliminates the window where "other threads can access the list after the lock is released."&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Insight
&lt;/h3&gt;

&lt;p&gt;What's the essence of this bug?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's not that unsafe itself is problematic, but that the invariant assumption in the SAFETY comment was wrong.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The developer assumed "the node is either in this list or in no list," but in reality, the &lt;code&gt;Node::release&lt;/code&gt; implementation could move nodes to a temporary list that wasn't protected by the lock. This broke the original assumption.&lt;/p&gt;

&lt;p&gt;In the words of Ralf Jung (Rust language team member): &lt;strong&gt;The most important comment in unsafe code is the one about its invariants.&lt;/strong&gt; And this comment was precisely wrong.&lt;/p&gt;




&lt;h2&gt;
  
  
  Part Two: What Exactly Is Unsafe Rust?
&lt;/h2&gt;

&lt;p&gt;Coincidentally, shortly before this CVE was published, Ralf Jung—an authoritative researcher on Rust's memory model—gave a talk about &lt;a href="https://www.youtube.com/watch?v=YwABQ9eYQv4" rel="noopener noreferrer"&gt;unsafe Rust at Scala Days 2025&lt;/a&gt;. This talk perfectly answers the question "What exactly is Unsafe?"&lt;/p&gt;

&lt;h3&gt;
  
  
  unsafe ≠ Unsafety
&lt;/h3&gt;

&lt;p&gt;First, let's clarify a common misconception: &lt;strong&gt;unsafe doesn't mean "unsafe code," but rather "code whose safety the compiler cannot automatically verify."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Safe Rust, the compiler can prove at compile time—through the ownership system, borrow checker, and other mechanisms—that code won't exhibit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Null pointer dereferences&lt;/li&gt;
&lt;li&gt;Dangling pointers&lt;/li&gt;
&lt;li&gt;Data races&lt;/li&gt;
&lt;li&gt;Buffer overflows&lt;/li&gt;
&lt;li&gt;Double frees&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But some operations cannot be statically proven safe by the compiler, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dereferencing raw pointers&lt;/li&gt;
&lt;li&gt;Calling FFI functions&lt;/li&gt;
&lt;li&gt;Accessing mutable static variables&lt;/li&gt;
&lt;li&gt;Implementing certain special traits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These operations require the &lt;code&gt;unsafe&lt;/code&gt; keyword. &lt;strong&gt;Unsafe doesn't mean "you're allowed to write dangerous code," but rather "you're promising the compiler: I have manually verified this code's safety."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ralf Jung's original words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Rust doesn't make unsafe 'free'; it makes it explicit and accountable."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Five Operations unsafe Allows
&lt;/h3&gt;

&lt;p&gt;Inside an &lt;code&gt;unsafe&lt;/code&gt; block, you can perform these five types of operations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dereference raw pointers&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;*const T&lt;/code&gt; and &lt;code&gt;*mut T&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Call unsafe functions or methods&lt;/td&gt;
&lt;td&gt;Including FFI functions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access or modify mutable static variables&lt;/td&gt;
&lt;td&gt;&lt;code&gt;static mut&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implement unsafe traits&lt;/td&gt;
&lt;td&gt;Such as &lt;code&gt;Send&lt;/code&gt;, &lt;code&gt;Sync&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access union fields&lt;/td&gt;
&lt;td&gt;Because the compiler can't know which variant is currently stored&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Key point: unsafe is "permission," not "correctness."&lt;/strong&gt; The compiler allows you to do these things, but you must ensure you don't trigger undefined behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Invariants: The Soul of unsafe
&lt;/h3&gt;

&lt;p&gt;Every piece of unsafe code should have clearly documented &lt;strong&gt;invariants&lt;/strong&gt;. Invariants answer three questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Why is unsafe needed?&lt;/strong&gt; What is this code doing that the compiler can't verify?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What assumptions must be maintained?&lt;/strong&gt; What conditions does correct execution depend on?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What could break it?&lt;/strong&gt; What inputs or concurrent accesses might violate these assumptions?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Looking back at CVE-2025-68260's problematic code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SAFETY: A `NodeDeath` is never inserted into the death_list&lt;/span&gt;
&lt;span class="c1"&gt;// of any node other than its owner, so it is either in this&lt;/span&gt;
&lt;span class="c1"&gt;// death list or in no death list.&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;node_inner&lt;/span&gt;&lt;span class="py"&gt;.death_list&lt;/span&gt;&lt;span class="nf"&gt;.remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comment does state "why it's safe," but the assumption itself is incomplete—it didn't consider that &lt;code&gt;Node::release&lt;/code&gt; would move nodes to a temporary list. &lt;strong&gt;If the invariants are wrong, the unsafe code cannot possibly be safe.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Part Three: Why Must the Kernel Use unsafe?
&lt;/h2&gt;

&lt;p&gt;"If unsafe has risks, why not avoid it entirely?"&lt;/p&gt;

&lt;p&gt;The answer is: &lt;strong&gt;In systems programming, unsafe is unavoidable.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Three Hard Requirements of Systems Programming
&lt;/h3&gt;

&lt;p&gt;Ralf Jung listed three fundamental reasons why unsafe exists:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Interacting with the Low-Level World&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Operating system kernels must interact with hardware, C code, and device drivers. These interactions require:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Raw pointer operations&lt;/li&gt;
&lt;li&gt;Specific memory layout control&lt;/li&gt;
&lt;li&gt;FFI calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust's safe abstractions are built on top of these low-level operations, and these operations themselves cannot be covered by safe abstractions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Performance and Predictability&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Certain high-performance data structures (like lock-free queues, memory allocators, intrusive linked lists) require fine-grained control over memory layout and access patterns. The compiler's automatic checks sometimes prevent safe but efficient implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Expressing Cross-Layer Invariants&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some safety guarantees span multiple abstraction layers that the compiler cannot statically capture. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"This pointer points to valid memory because we guarantee it at a higher logical level"&lt;/li&gt;
&lt;li&gt;"This trait implementation definitely satisfies certain semantic constraints"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ralf Jung's summary:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Unsafe is where Rust meets the world."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Linux Kernel's Special Challenges
&lt;/h3&gt;

&lt;p&gt;As a complex piece of systems software, the Linux kernel has numerous scenarios requiring unsafe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory management&lt;/strong&gt;: Direct manipulation of physical memory and page tables&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interrupt handling&lt;/strong&gt;: Executing code in special contexts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency primitives&lt;/strong&gt;: Implementing locks, atomic operations, RCU&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device drivers&lt;/strong&gt;: Interacting with hardware registers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C code interoperation&lt;/strong&gt;: The vast majority of the kernel is still C&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This isn't Rust's problem—it's the nature of systems programming. C's approach is &lt;strong&gt;unsafe by default&lt;/strong&gt;—all code might have problems, with no compile-time checks. Rust's approach is &lt;strong&gt;safe by default + explicit unsafe (&lt;code&gt;unsafe&lt;/code&gt; block &amp;amp; keyword)&lt;/strong&gt;—concentrating risk at a few controlled boundaries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Part Four: How to Use unsafe Correctly
&lt;/h2&gt;

&lt;p&gt;Since &lt;code&gt;unsafe&lt;/code&gt; is unavoidable, how do we minimize risk?&lt;/p&gt;

&lt;p&gt;Ralf Jung offered three core principles: &lt;strong&gt;Minimize, Document, Encapsulate&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Principle One: Minimize
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Use unsafe only where unavoidable, keeping it to the smallest possible scope.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don't write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// A whole bunch of code&lt;/span&gt;
    &lt;span class="c1"&gt;// Only one line actually needs unsafe&lt;/span&gt;
    &lt;span class="c1"&gt;// But the entire block is marked unsafe&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Safe preparation work&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_pointer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_length&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Use unsafe only where necessary&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Safe follow-up processing&lt;/span&gt;
&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Principle Two: Document
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Write clear invariants above every unsafe block.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A good comment template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SAFETY: &lt;/span&gt;
&lt;span class="c1"&gt;// - `ptr` is valid because it came from `Box::into_raw` and hasn't been freed yet&lt;/span&gt;
&lt;span class="c1"&gt;// - `ptr` points to properly aligned memory because Box guarantees this&lt;/span&gt;
&lt;span class="c1"&gt;// - We have exclusive access because no other references exist&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bad comments (or no comments at all) lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Future maintainers not knowing what assumptions the code depends on&lt;/li&gt;
&lt;li&gt;Refactoring potentially breaking assumptions inadvertently&lt;/li&gt;
&lt;li&gt;Inability to verify correctness during code review&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Principle Three: Encapsulate
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Wrap unsafe implementations with safe APIs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the most important principle. Unsafe code should be encapsulated inside modules, exposing &lt;strong&gt;completely safe&lt;/strong&gt; interfaces externally. Users don't need to know there's unsafe inside and cannot misuse it.&lt;/p&gt;

&lt;p&gt;The classic example is the standard library's &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Internal implementation uses unsafe for memory management&lt;/span&gt;
&lt;span class="c1"&gt;// But the external interface is 100% safe&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Safe call, no unsafe needed&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;  &lt;span class="c1"&gt;// Safe call&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Vec&lt;/code&gt; implementer bears the responsibility of unsafe; users don't need to worry about memory safety at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Architecture Diagram: Proper Layering of unsafe
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────────────────────┐
│                   Application Layer Code                     │
│                     (100% Safe Rust)                         │
├─────────────────────────────────────────────────────────────┤
│                      Safe API Layer                          │
│            (Safe interface, encapsulating unsafe)            │
├─────────────────────────────────────────────────────────────┤
│                     unsafe Core Layer                        │
│      (Minimized unsafe code, detailed invariant docs)        │
├─────────────────────────────────────────────────────────────┤
│                  Low-level / FFI / Hardware                  │
└─────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key insight: The smaller the unsafe surface area, the lower the audit cost, and the easier bugs are to find.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Part Five: Tools and Methods for Making unsafe Verifiable
&lt;/h2&gt;

&lt;p&gt;The Rust community hasn't stopped at "relying on human effort to ensure unsafe correctness." A series of tools and formal methods are shifting unsafe risk management from "experience-driven" to "evidence-driven."&lt;/p&gt;

&lt;h3&gt;
  
  
  Miri: The Gatekeeper of unsafe
&lt;/h3&gt;

&lt;p&gt;Miri is a Rust interpreter that can detect various undefined behaviors at runtime:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invalid memory accesses&lt;/li&gt;
&lt;li&gt;Aliasing rule violations (Stacked Borrows / Tree Borrows)&lt;/li&gt;
&lt;li&gt;Uninitialized memory reads&lt;/li&gt;
&lt;li&gt;Memory leaks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When developing unsafe code, running tests with Miri can catch many hidden bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loom: The Concurrency Revealer
&lt;/h3&gt;

&lt;p&gt;Loom is a concurrency testing framework that discovers race conditions by exhaustively (or intelligently sampling) thread schedules.&lt;/p&gt;

&lt;p&gt;The bug in &lt;code&gt;CVE-2025-68260&lt;/code&gt; would likely have been found before release if tested with Loom.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sanitizers: Sentinels at FFI Boundaries
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;AddressSanitizer&lt;/code&gt; (&lt;code&gt;ASan&lt;/code&gt;) and &lt;code&gt;ThreadSanitizer&lt;/code&gt; (&lt;code&gt;TSan&lt;/code&gt;) can detect memory errors and data races, particularly useful for Rust-C interaction boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  RustBelt: Formal Verification
&lt;/h3&gt;

&lt;p&gt;RustBelt is an academic project providing formal semantic foundations for Rust's type system and unsafe encapsulation. It mathematically proves: &lt;strong&gt;If unsafe code satisfies specific invariants, then its safe encapsulation is safe in any usage scenario.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ralf Jung's original words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Tooling makes unsafe less mystical; formal methods make it trustworthy."&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Part Six: A Rational View of This CVE
&lt;/h2&gt;

&lt;p&gt;Now, let's return to the original debate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let the Data Speak
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;On the same day&lt;/strong&gt; &lt;code&gt;CVE-2025-68260&lt;/code&gt; was published, the C portion of the Linux kernel had &lt;strong&gt;159 CVEs&lt;/strong&gt; issued.&lt;/p&gt;

&lt;p&gt;Let's make a simple comparison:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Rust Code&lt;/th&gt;
&lt;th&gt;C Code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time in kernel&lt;/td&gt;
&lt;td&gt;~3 years&lt;/td&gt;
&lt;td&gt;~33 years&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code proportion&lt;/td&gt;
&lt;td&gt;&amp;lt; 1%&lt;/td&gt;
&lt;td&gt;&amp;gt; 99%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cumulative CVE count&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Tens of thousands&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVEs issued this time&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;159&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;This data isn't meant to prove "Rust is perfect," but to show: Rust significantly reduces the occurrence rate of memory safety vulnerabilities.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does This Bug Tell Us?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unsafe code needs more careful review&lt;/strong&gt;: Safety assumptions in comments must cover all scenarios, including concurrent scenarios.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rust hasn't eliminated bugs, but changed their distribution&lt;/strong&gt;: Safe Rust eliminates entire classes of memory safety bugs; remaining bugs concentrate at unsafe boundaries and logical errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit unsafe makes problems easier to locate&lt;/strong&gt;: Once this bug was found, developers immediately knew to check the unsafe block's invariants. With C code, locating the problem would be much harder.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Rust's Promise Was Never "Zero Bugs"
&lt;/h3&gt;

&lt;p&gt;Rust's safety promise is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If your code is 100% safe Rust (no unsafe), it won't have memory safety issues.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For code containing unsafe, the promise becomes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The correctness of unsafe code depends on whether your maintained invariants are correct. If invariants are correct, the encapsulated safe API is safe.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This CVE precisely proves: &lt;strong&gt;When invariants are wrong, unsafe code will have problems.&lt;/strong&gt; This isn't a Rust defect—it's Rust's design working as intended—concentrating risk in auditable, accountable places.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison with C
&lt;/h3&gt;

&lt;p&gt;What would it look like if the same code were implemented in C?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No unsafe markers&lt;/strong&gt;: All code is "unsafe," making it impossible to quickly locate high-risk areas&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No mandatory SAFETY comments&lt;/strong&gt;: Invariants might not be documented at all&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More prone to other memory safety issues&lt;/strong&gt;: use-after-free, buffer overflow, null pointer, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Harder to trace problem sources&lt;/strong&gt;: No ownership system to assist analysis&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Part Seven: Responding to Rust Haters
&lt;/h2&gt;

&lt;p&gt;Let's directly address the criticism from the beginning of this article:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Since unsafe is unavoidable in the kernel, and unsafe code has hidden dangers, maybe you shouldn't have been bragging everywhere that 'if it compiles, it works'..."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;This criticism is based on a false premise.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Rust community has never said "if it compiles, it works." The accurate statement is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Safe Rust code&lt;/strong&gt;: Compilation passing = no memory safety issues (this has formal proof support)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unsafe Rust code&lt;/strong&gt;: Compilation passing ≠ no problems; human verification of invariants is required&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mixed code&lt;/strong&gt;: Overall safety depends on the correctness of the unsafe portions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rust's value lies in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Narrowing the scope requiring human review&lt;/strong&gt;: Only unsafe blocks need special attention&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clarifying review focus&lt;/strong&gt;: Invariant documentation tells you what to verify&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eliminating entire classes of bugs&lt;/strong&gt;: In safe Rust, data races, use-after-free, etc. simply cannot occur&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reducing "100% of code needs memory safety concerns" to "only 1% of code needs concerns"—this itself is enormous progress.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion: Safety Isn't About Avoiding Low-Level Work, But Doing It the Right Way
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CVE-2025-68260&lt;/code&gt; is the first CVE since Rust entered the Linux kernel, but it won't be the last. As long as there's unsafe code, there's potential for mistakes.&lt;/p&gt;

&lt;p&gt;But this precisely illustrates Rust's design philosophy:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Acknowledge the necessity of low-level operations, but use explicit markers, invariant documentation, tooling detection, and community conventions to control risk to the minimum scope.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ralf Jung's summary at the end of his talk is worth remembering for every systems programmer:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;unsafe is a necessary interface layer, not wanton escape&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Document and encapsulate with invariants at the core, keeping danger in a small room&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Treat UB as a red line, guarding it with tools and tests&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use community conventions and formal methods to upgrade experience into verifiable engineering&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Safety isn't about avoiding low-level work, but doing it the right way.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This CVE isn't Rust's failure—it's the unsafe mechanism working as designed—exposing problems at controlled boundaries, allowing us to quickly locate, fix, and learn from them.&lt;/p&gt;

&lt;p&gt;159 versus 1. This numerical comparison is the story truly worth discussing today.&lt;/p&gt;




&lt;h2&gt;
  
  
  Appendix: CVE-2025-68260 Technical Details
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vulnerability Type&lt;/strong&gt;: Race Condition&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Affected Versions&lt;/strong&gt;: Linux 6.18 (introduced)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fixed Versions&lt;/strong&gt;: Linux 6.18.1, 6.19-rc1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem File&lt;/strong&gt;: &lt;code&gt;drivers/android/binder/node.rs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Root Cause&lt;/strong&gt;: &lt;code&gt;Node::release&lt;/code&gt; iterates over a temporary list after releasing the lock, creating a data race with other threads' unsafe remove operations&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;: Pop elements directly from the original list while holding the lock, avoiding the race window after lock release&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference Links&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cve.org/CVERecord/?id=CVE-2025-68260" rel="noopener noreferrer"&gt;CVE Announcement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://git.kernel.org/stable/c/3428831264096d32f830a7fcfc7885dd263e511a" rel="noopener noreferrer"&gt;Fix Commit 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://git.kernel.org/stable/c/3e0ae02ba831da2b707905f4e602e43f8507b8cc" rel="noopener noreferrer"&gt;Fix Commit 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;If you found this article helpful, feel free to like, share, and subscribe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What are your thoughts on Rust and systems programming? Feel free to discuss in the comments.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Forging the Future: My Ten-Year Journey Growing with Rust</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Mon, 19 May 2025 20:42:00 +0000</pubDate>
      <link>https://dev.to/zhanghandong/forging-the-future-my-ten-year-journey-growing-with-rust-50f1</link>
      <guid>https://dev.to/zhanghandong/forging-the-future-my-ten-year-journey-growing-with-rust-50f1</guid>
      <description>&lt;p&gt;Hello everyone, I am Zhang Handong(AlexZhang), an independent consultant and technical writer. I have been in the Rust community for ten years, and on the occasion of Rust's tenth anniversary, I want to write down my story. It may not be that extraordinary, but these ten years are where I come from today.&lt;/p&gt;

&lt;p&gt;Looking back on this journey, I see not just the evolution of a programming language, but the growth of a passionate community from nothing to something, from small to large. During these ten years, I experienced the transformation from a learner to an evangelist, and witnessed the entire process of Rust in China evolving from a niche language to gradual adoption by mainstream enterprises. This is a story about learning, sharing, challenges, and growth, and also a microcosm of Chinese developers participating in the global open source community.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In fact, I've experienced many people and events related to Rust over these ten years that cannot be contained in this short article. Perhaps you will see some content worth writing about in my new book.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let me start from 2015.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss0ddqilbrtwhq9tczse.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss0ddqilbrtwhq9tczse.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  First Encounter with Rust: The Turning Point of 2015
&lt;/h2&gt;

&lt;p&gt;In 2015, I began to explore the Rust programming language, which had just released version 1.0.&lt;/p&gt;

&lt;p&gt;Actually, before 2015, I had been using dynamic languages for application development, coinciding with the previous "startup wave." But by 2015, I felt personally exhausted with application development because applications changed too quickly, so I wanted to explore lower-level systems. I had this idea for a long time, but because I didn't particularly like C/C++, I never took action. After Rust released version 1.0 in 2015, I began learning Rust.&lt;/p&gt;

&lt;p&gt;As an engineer with many years of software development experience, I was attracted to Rust's design philosophy: memory safety without garbage collection, concurrency without data races, and zero-cost abstractions. My experience working in e-commerce, social gaming, advertising, and crowdfunding made me deeply aware of the limitations of traditional languages in large-scale system development, while Rust's future-oriented design appealed to me. At that time, I believed that Rust would be the last programming language I would need to learn in my lifetime.&lt;/p&gt;

&lt;p&gt;However, the learning process for Rust was full of challenges. I once considered giving up, but my character of enjoying challenges pushed me to persist. And it was precisely these seemingly difficult learning curves that solidified my knowledge foundation in lower-level systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I have always believed that it was Rust that reignited my passion for programming.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Evangelism Path: From Daily Reports to Writing Books
&lt;/h2&gt;

&lt;p&gt;In January 2018, I created "Rust Daily," compiling the latest developments in the Rust ecosystem every day. At that time, resources about Rust in Chinese were extremely limited. I hoped that through daily reports, I could lower the barrier for Chinese developers to access information about Rust. To my surprise, this small initiative received an enthusiastic response from the community and continues to this day.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Birth and Impact of "The Way of Rust Programming"
&lt;/h3&gt;

&lt;p&gt;In January 2019, my book "The Way of Rust Programming" was officially published. Reflecting on the original intention of writing, it actually stemmed from my understanding of learning itself. Rust is known for its steep learning curve, but in my view, this is precisely a valuable growth opportunity, not an obstacle. I didn't see it as a bad thing; I am the type of person who likes to challenge difficulties. A high learning curve indicated gaps in my knowledge system, and challenging the Rust language was an excellent opportunity for me to supplement my foundational knowledge of computer systems.&lt;/p&gt;

&lt;p&gt;Rust learning materials on the market were very limited at that time. The official "The Rust Programming Language," while comprehensively introducing the syntax, failed to help readers clarify Rust's knowledge system. I had to resort to C++ and Haskell materials to assist my learning (because Rust borrowed many features from these two languages). As my understanding of Rust deepened, the idea of writing a systematic Rust guide gradually took shape.&lt;/p&gt;

&lt;p&gt;On one hand, I firmly believe that "writing is the best way of thinking," and through writing, I could systematically organize my understanding of Rust; on the other hand, I observed that the community indeed needed a book for advanced readers, providing those who had completed basic knowledge with a more systematic cognitive perspective.&lt;/p&gt;

&lt;p&gt;After "The Way of Rust Programming" was published, my influence in the Rust community significantly increased, and that same year I participated in RustAsiaCon to share and exchange ideas. However, as no one is perfect, errors in the book were gradually discovered by readers. In my GitHub errata repository, readers submitted more than 200 issues, and some even mocked: "How can you read a book with more than 200 issues of errata?"&lt;/p&gt;

&lt;p&gt;These criticisms once troubled me greatly, but later I realized that &lt;strong&gt;a book is essentially a form of communication&lt;/strong&gt;. For me, &lt;strong&gt;writing a book is not about educating others, but about sharing my understanding while accepting feedback to promote my own growth&lt;/strong&gt;. Through readers' errata, I corrected some misconceptions about Rust, which is exactly the best feedback I hoped to see from writing the book.&lt;/p&gt;

&lt;p&gt;With fame came more attention and evaluation. For a period, I was overly concerned with others' evaluations. When someone sarcastically called me the "Father of Rust in China," I once fell into depression, almost falling into the "self-verification trap." After reflection, I finally understood: &lt;strong&gt;regardless of how others evaluate me, I only need to focus on doing my best&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Community Building: Connecting China with the World
&lt;/h2&gt;

&lt;p&gt;In 2020, despite the challenges of the global pandemic, my teammates and I organized the first RustChinaConf in Shenzhen. As one of the initiators of the conference, seeing hundreds of Rust enthusiasts gather together to passionately discuss technical topics made me incredibly proud. That same year, I released "Zhang Handong's Rust Practical Course" on GeekTime, hoping to help more developers systematically learn Rust through online education.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Founding and Reflection of "Rust Magazine"
&lt;/h3&gt;

&lt;p&gt;In 2021, the successful hosting of RustChinaConf showed me the vibrant vitality of the Chinese Rust community and gave me more confidence to promote community development. I decided to found "Rust Magazine," hoping that through a systematic publication, Chinese companies adopting Rust could better present their practical experiences to the public while providing a continuous learning platform for the community.&lt;/p&gt;

&lt;p&gt;That year, I also established the "Rust Chinese Community" on Feishu and regularly organized online salon activities. These platforms not only provided learning resources for developers but also facilitated connections between many Rust startups and talents. I was also fortunate to participate in Alibaba DAMO Academy's Feitian School to share Rust concepts, bringing Rust philosophy into large technology companies.&lt;/p&gt;

&lt;p&gt;Unfortunately, "Rust Magazine" ceased publication after persisting for a year. Reflecting on this experience, I believe it may have been because Rust's industrial adoption in China had not yet reached a critical point, and the pressure of content creation and maintenance was substantial. Nevertheless, I still believe this was a valuable attempt, and perhaps by 2026, with the further popularization of Rust in China, we can restart this project.&lt;/p&gt;

&lt;p&gt;In 2022, due to the pandemic, RustChinaConf was held online. That same year, I open-sourced the "Rust Coding Guidelines" project, hoping to provide a coding standard blueprint for Chinese companies adopting Rust.&lt;/p&gt;

&lt;h3&gt;
  
  
  Industrial Adoption: From Theory to Practice
&lt;/h3&gt;

&lt;p&gt;Reflecting on my experiences in community evangelism, RustChinaConf conferences, and providing Rust consulting services to different enterprises, I found a clear pattern: &lt;strong&gt;Rust adoption in China shows distinct phases&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have successfully provided Rust consulting and internal training for these enterprises: Huawei / ZTE / Ping An Technology / Zhongtai Securities / Li Auto / Pengcheng Laboratory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2015-2018 was the exploration period, when everyone was full of hope for the Rust language but also had many concerns. However, domestic New SQL database pioneer PingCAP and ByteDance's Feishu team were among the earliest "early adopters."&lt;/p&gt;

&lt;p&gt;2019-2022 was the expansion period, with telecommunications giants like Huawei/ZTE beginning to prepare for large-scale adoption of Rust. Huawei piloted Rust adoption in multiple areas, even making it one of the important development languages for the HarmonyOS operating system. During this phase, Rust was mainly applied in system programming, device drivers, and network services. And in 2021, Huawei joined the Rust Foundation as a founding board member. Tsinghua University's Rust OS Training Camp also began to launch.&lt;/p&gt;

&lt;p&gt;2022 to the present is the explosive period. ByteDance also began to gradually expand Rust internally, not just in Feishu but also in infrastructure, and by 2024, TikTok was also using Rust. By 2024, Huawei had designated Rust as the company's seventh main language. Ant Group also began using Rust to write a trusted OS, with the goal of replacing Linux. Many infrastructure startup teams, from embedded systems to databases, from physics engines to GUI to apps, have companies and teams using Rust.&lt;/p&gt;

&lt;h3&gt;
  
  
  Open Source Contributions: Connecting Technology and Community
&lt;/h3&gt;

&lt;p&gt;In recent years, I have gradually shifted my focus to open source project development and contributions. Participating in the development of the Robius framework (a pure Rust App development framework targeting Flutter), Moxin (a large model client based on Rust), and other projects has given me the opportunity to apply Rust to cutting-edge technology fields.&lt;/p&gt;

&lt;p&gt;In 2023 and 2024, I participated in hosting the GOSIM Rust Workshop and GOSIM China Rust Track. GOSIM invited Rust officials and experts from well-known open source projects abroad to come to China to share. These international exchange activities not only enhanced China's influence in the global Rust community but also provided Chinese developers with opportunities for face-to-face exchanges with top international Rust experts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This September, in Hangzhou, GOSIM will join with RustChinaConf 2025 and RustGlobal (the Rust Foundation's conference brand) to build a Rust exchange platform for domestic and international Rust developers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In 2025, I collaborated with Tsinghua OS Training Camp to launch the "Rust Open Training Camp," which received event sponsorship from the Rust Foundation. This project aims to cultivate Rust talents with system programming capabilities, filling the talent gap in this field in China. The first training camp was successfully held, with registrations reaching 2,000 people.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Outlook: Continuing to Expand the Scale of Domestic Rust Developers
&lt;/h2&gt;

&lt;p&gt;Looking back on this ten-year journey with Rust, I feel deeply honored to have witnessed and participated in the development of Rust in China. From an initially niche language to its gradual application in cutting-edge fields such as systems programming, cloud native, AI, and blockchain, Rust's growth trajectory is exciting.&lt;/p&gt;

&lt;p&gt;As a Rust evangelist, my greatest sense of achievement comes not from personal technical progress, but from seeing more and more Chinese developers join the Rust community, more and more Chinese enterprises adopt the Rust technology stack, and more and more Chinese faces appear in the global Rust community.&lt;/p&gt;

&lt;p&gt;In the future, I will continue to contribute to Rust technology promotion, community building, and talent cultivation, helping the continued development of Rust in China, and also looking forward to Chinese developers playing a greater role in the global Rust ecosystem.&lt;/p&gt;

&lt;p&gt;Ten years to forge a sword, the path of Rust is long. But I firmly believe that the best times are still ahead.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>AgentKit: A Technical Vision for Building Universal AI Automation for Human-Computer Interaction Based on Rust</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Fri, 18 Apr 2025 04:59:00 +0000</pubDate>
      <link>https://dev.to/zhanghandong/agentkit-a-technical-vision-for-building-universal-ai-automation-for-human-computer-interaction-2523</link>
      <guid>https://dev.to/zhanghandong/agentkit-a-technical-vision-for-building-universal-ai-automation-for-human-computer-interaction-2523</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This week I came across a project called &lt;a href="https://github.com/droidrun/droidrun" rel="noopener noreferrer"&gt;Droidrun&lt;/a&gt;, which allows you to control your Android phone through natural language commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdkxgvdjmtvojl2ymkwpc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdkxgvdjmtvojl2ymkwpc.png" alt=" " width="406" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I first saw this project, I didn't think much of it. Today, after seeing the news about the project being open-sourced, I became curious about how it works, so I looked at the code to understand the principles behind it.&lt;/p&gt;

&lt;p&gt;What I found was truly fascinating.&lt;/p&gt;

&lt;p&gt;Just this Monday, I had come across &lt;a href="https://accesskit.dev/" rel="noopener noreferrer"&gt;Accesskit.dev&lt;/a&gt;, a cross-platform, cross-language Rust abstraction layer that encapsulates the native accessibility service APIs of different operating systems (like Windows, macOS, Linux/Unix, Android), such as UIA, NSAccessibility, AT-SPI, and the Android Accessibility Framework. At that time, I was thinking that if large language models were to act as humans, they would essentially be like people with disabilities (no derogatory meaning intended). This API set would be perfect for building AI Agents.&lt;/p&gt;

&lt;p&gt;And today, I discovered that the core mechanism of the Droidrun project is built using Android's accessibility service API. This is what made me feel that the world is truly amazing: while I was still at the idea stage, someone else had already implemented it.&lt;/p&gt;

&lt;p&gt;Unfortunately, it's not a cross-platform app, and its limitation is that it only supports Android phones. Coincidentally, I am a number one fan of the Rust language, and I know that Rust is particularly well-suited for cross-platform development.&lt;/p&gt;

&lt;p&gt;I started thinking, &lt;strong&gt;could we take the approach from the Droidrun project, combine it with the Rust language, and implement a universal AI automation kit that not only supports Android phones but also iOS, desktop platforms, and even any smart terminal?&lt;/strong&gt; This article was born from this idea, and &lt;strong&gt;AgentKit&lt;/strong&gt; is the name I've given to this universal AI automation kit.&lt;/p&gt;

&lt;p&gt;Therefore, this article will start with the Android platform's AI automation practice, Droidrun.ai, deeply analyze its implementation mechanism and limitations. We will then explore the key role of cross-platform accessibility infrastructure AccessKit. Finally, I will propose a detailed vision for the universal AI control framework AgentKit, including its architecture design, collaborative relationship with existing protocols, potential application scenarios, and development roadmap, aiming to outline a future automation infrastructure driven by AI that transcends digital boundaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Future of Applications in the AI Era&lt;/li&gt;
&lt;li&gt;Analysis: Droidrun AI's Pioneering Exploration of Android Automation&lt;/li&gt;
&lt;li&gt;Foundation of the AgentKit Vision: Cross-Platform Capabilities of AccessKit&lt;/li&gt;
&lt;li&gt;AgentKit: Universal AI Automation Framework Concept&lt;/li&gt;
&lt;li&gt;Complementary Collaboration Between AgentKit and Claude MCP / Google A2A Protocols&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Future of Applications in the AI Era
&lt;/h2&gt;

&lt;p&gt;Before thinking about this universal AI automation kit, the first question that came to my mind was: &lt;strong&gt;Do we still need apps in the AI era?&lt;/strong&gt; After all, if we don't need apps anymore, we wouldn't need any AI automation kit either.&lt;/p&gt;

&lt;p&gt;Fortunately, I understand an objective principle of this world: &lt;strong&gt;castles in the air don't exist&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, let's think about this question starting from the evolution history of computer interfaces. Human-computer interaction has undergone several major paradigm shifts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Command Line Interface&lt;/strong&gt;: Humans needed precise syntax and memory capabilities, not something an average person could operate (I've heard that the father of Genshin Impact could open game programs in DOS at the age of five).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graphical User Interface (GUI)&lt;/strong&gt;: Introduced visual metaphors and direct manipulation concepts (which saved ordinary people like me).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mobile Touch Interface&lt;/strong&gt;: Brought computing power into our palms, based on gesture interaction (iPhone is great in every way, just a bit expensive).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Voice Assistants&lt;/strong&gt;: Started moving toward natural language interaction (AI is quite clever).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And now we are entering an era of the so-called &lt;strong&gt;"AI Intermediary Interface"&lt;/strong&gt;, where large language models act as intermediaries between human intent and computing resources. This transition is indeed revolutionizing the way we interact with technology, but it doesn't mean apps will completely disappear.&lt;/p&gt;

&lt;p&gt;Despite AI's improving language understanding capabilities, I believe applications will transform rather than disappear, for several reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In terms of cognitive and perceptual efficiency, AI cannot replace humans&lt;/strong&gt;. Humans process visual information with extreme efficiency. Our brains can instantly understand complex spatial relationships, hierarchical structures, and patterns, while describing these using language (text or speech) might require lengthy passages. Imagine editing a photo: describing the precise adjustments you want ("increase brightness by 15% in the upper right quadrant while reducing saturation in the blue channel") is much more complex and cognitively demanding than simply dragging sliders or using visual tools. Language, though powerful, is not the optimal choice for all interaction scenarios. Just as we use both verbal and non-verbal communication (gestures, expressions) in real life, human-computer interaction will maintain a state where multiple modes coexist.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Some professional domains have special requirements&lt;/strong&gt;. Many fields need specialized interfaces that match their conceptual models, such as:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creative applications&lt;/strong&gt;: Music production, video editing, and 3D modeling involve spatial and temporal relationships that are naturally expressed through visual interfaces&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data visualization&lt;/strong&gt;: Understanding complex data often requires interactive exploration, which is difficult to navigate through language alone&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Games&lt;/strong&gt;: Interactive entertainment relies on immediate feedback and spatial awareness&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Professional tools&lt;/strong&gt;: Fields like medical imaging and architectural design require highly visual and precise control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these two fundamental reasons, I believe applications won't disappear but will deeply integrate AI while retaining visual elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AI-enhanced interfaces&lt;/strong&gt;: Traditional visual elements enhanced by AI capabilities, like Photoshop's generative fill feature&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multimodal interaction&lt;/strong&gt;: Combining voice, touch, eye movement, and gestures to form a richer interactive experience&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adaptive interfaces&lt;/strong&gt;: User interfaces that automatically reconfigure based on user behavior and AI understanding (this idea might be somewhat ahead of its time)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From a cognitive science perspective, human thinking processes both linguistic and non-linguistic information simultaneously, and our technological interfaces will reflect this duality. Language models provide a natural instruction layer, while visual interfaces provide the interaction modes required for spatial and visual thinking.&lt;/p&gt;

&lt;p&gt;Therefore, universal AI automation kits like AgentKit may become even more important in this evolving process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Need for bridging technology&lt;/strong&gt;. As interface paradigms diversify, we need systems that can work across different interaction models and device platforms. AgentKit's vision of providing a unified layer for AI to interact with various interfaces becomes more valuable, not less. &lt;strong&gt;This bridging role is similar to the relationship between operating systems and hardware&lt;/strong&gt;. When hardware becomes more diverse, the operating system's abstraction layer doesn't become less important; rather, it becomes more critical as it needs to coordinate more types of devices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Managing complexity through abstraction&lt;/strong&gt;. Even if frontend interfaces change significantly, the basic need to interact with complex systems remains. AgentKit provides an abstraction layer that allows users and AI to avoid focusing too much on implementation details, which is very important. &lt;strong&gt;We can draw an analogy to the evolution of programming languages&lt;/strong&gt;: although high-level languages continue to evolve, the complexity of underlying systems still exists; we just manage this complexity through better abstractions. Similarly, the evolution of AI interfaces won't eliminate the complexity of underlying systems but will create better abstractions to manage it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supporting the transition period&lt;/strong&gt;. The shift to AI-first computing will be gradual and uneven. Systems like AgentKit that can connect traditional and emerging paradigms are crucial during this long transition period. &lt;strong&gt;History shows that technological transitions usually take longer than we expect&lt;/strong&gt;. For example, command-line interfaces still exist today, decades after graphical interfaces became popular, and maintain their advantages in certain domains. Similarly, traditional application design will coexist with the AI era for a long time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;I believe we will see layered user experiences&lt;/strong&gt;: at the high level, users can express their intent through natural language ("edit my photo from yesterday to make it look warmer"), while AI is responsible for translating this intent into specific operations, or opening appropriate visual interfaces when needed for fine-tuning or intuitive feedback.&lt;/p&gt;

&lt;p&gt;A characteristic of technological evolution is that &lt;strong&gt;new paradigms rarely completely replace their predecessors&lt;/strong&gt;. As I mentioned earlier, castles in the air don't exist in this world, and AI is no exception. Instead, they usually create new ecological niches, while old methods continue to exist in their areas of expertise.&lt;/p&gt;

&lt;p&gt;Therefore, the future is unlikely to be a binary choice between traditional applications and pure AI interfaces, but rather a rich continuum where different interaction styles coexist, complement, and co-evolve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis: Droidrun AI's Pioneering Exploration of Android Automation
&lt;/h2&gt;

&lt;p&gt;Droidrun.AI has opened a window into this new human-computer interaction paradigm. I believe it is an important attempt in the field of Android automation, demonstrating how to achieve intelligent device control based on natural language using AI and system-level APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Droidrun Overall Architecture and Working Principles
&lt;/h3&gt;

&lt;p&gt;The Droidrun App consists of two independent projects: &lt;code&gt;droidrun&lt;/code&gt; (Python agent) and &lt;code&gt;droidrun-portal&lt;/code&gt; (Android application). The overall workflow of the application is shown in the diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dq1bt8qglu393508xux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dq1bt8qglu393508xux.png" alt=" " width="788" height="1662"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flow Chart Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User Startup:&lt;/strong&gt; The user enters a natural language task through the Droidrun CLI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment Setup:&lt;/strong&gt; The CLI parses parameters, obtains the device serial number, and sets the environment variable &lt;code&gt;DROIDRUN_DEVICE_SERIAL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agent Startup:&lt;/strong&gt; The &lt;code&gt;ReActAgent&lt;/code&gt; is initialized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ReAct Loop:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Thinking:&lt;/strong&gt; The Agent sends the current goal and history steps to the &lt;code&gt;LLM Reasoner&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decision:&lt;/strong&gt; The LLM returns the thinking process (&lt;code&gt;thought&lt;/code&gt;), the action to be executed (&lt;code&gt;action&lt;/code&gt;), and parameters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Action:&lt;/strong&gt; The Agent calls the corresponding function in &lt;code&gt;tools.actions&lt;/code&gt; to execute the action.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Get UI State (Key Interaction):&lt;/strong&gt; If the action is &lt;code&gt;get_clickables&lt;/code&gt; or &lt;code&gt;get_all_elements&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;The Python tool sends a broadcast command to the Portal Service on the device via ADB.&lt;/li&gt;
&lt;li&gt;The Portal Service uses the accessibility API to scan the UI, build a hierarchy, assign indices, write the results to a JSON file, and print the file path via Logcat.&lt;/li&gt;
&lt;li&gt;The Python tool polls Logcat to get the file path, then pulls the JSON file via ADB, parses it, and returns it as an observation result to the Agent.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute Device Operations:&lt;/strong&gt; If the action is &lt;code&gt;tap&lt;/code&gt;, &lt;code&gt;swipe&lt;/code&gt;, &lt;code&gt;input_text&lt;/code&gt;, etc.:

&lt;ul&gt;
&lt;li&gt;The Python tool looks up the element in the cache (if it's &lt;code&gt;tap(index=...)&lt;/code&gt;), calculates coordinates, or constructs a command.&lt;/li&gt;
&lt;li&gt;It sends the corresponding &lt;code&gt;input&lt;/code&gt; or &lt;code&gt;am&lt;/code&gt; shell command directly to the device via ADB.&lt;/li&gt;
&lt;li&gt;It returns a message of operation success or failure as the observation result.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complete Task:&lt;/strong&gt; If the action is &lt;code&gt;complete&lt;/code&gt;, the task is marked as completed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observation:&lt;/strong&gt; The Agent receives the result of the action execution (UI data or operation status).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loop/End:&lt;/strong&gt; If the task is not completed and the maximum number of steps has not been reached, return to the "thinking" stage with the new observation result; otherwise, end the loop.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result Output:&lt;/strong&gt; The CLI displays the execution process or final result to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, let's look at the code for these two parts to understand their working mechanisms.&lt;/p&gt;

&lt;h4&gt;
  
  
  Droidrun (Python Agent)
&lt;/h4&gt;

&lt;p&gt;This Python package is the &lt;strong&gt;core control end (analogous to the brain and hands)&lt;/strong&gt; of the Droidrun system, responsible for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Agent Logic Implementation:&lt;/strong&gt; Contains intelligent decision-making and task execution processes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device Interaction Tools:&lt;/strong&gt; Provides tools for communicating with and controlling Android devices, mainly relying on ADB and the Droidrun Portal application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Command Line Interface:&lt;/strong&gt; Provides an entry point for user interaction with the Agent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Points of Core Components:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;adb&lt;/code&gt; Package:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Provides **asynchronous** encapsulation and execution of standard `adb` commands (such as `shell`, `pull`, `install`).
- The `Device` class represents a device, encapsulating specific operations such as `tap`, `swipe`, `input_text`, `take_screenshot`, etc., which are ultimately completed by executing the corresponding `adb shell` commands.
- `DeviceManager` is used to manage multiple device connections.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;tools&lt;/code&gt; Package:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- **Key Interface:** This is the **main bridge** between the Python Agent and the Android Portal application.
- **UI Perception (`get_clickables`/`get_all_elements`):**
    - **Triggers** the Portal application to scan the UI via `adb shell am broadcast`.
    - **Core Interaction Mechanism:** Polls `adb shell logcat -d` to **listen** for logs printed by Portal containing the JSON file path.
    - After obtaining the path, uses `adb pull` to **pull** the JSON file from the device.
    - Parses the JSON (processes the nested structure to **flatten** it), and **caches** the results (`CLICKABLE_ELEMENTS_CACHE`).
- **UI Operations (`tap`, etc.):**
    - `tap` (based on index) looks up elements in the cache, calculates coordinates, and ultimately executes `adb shell input tap`.
    - Other actions (`swipe`, `input_text`, etc.) are wrappers around `adb.Device` methods, with added error handling and specific logic (such as escaping and chunking for `input_text`).
- **`complete` Tool:** Used by the Agent to indicate to the ReAct loop that the task has been completed.
- Depends on the `DROIDRUN_DEVICE_SERIAL` environment variable to specify the target device.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;agent&lt;/code&gt; Package:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- **Core Logic:** Implements the **ReAct (Reasoning + Acting)** agent pattern.
- `LLMReasoner`: Encapsulates interaction with LLMs (OpenAI, Anthropic, Gemini), responsible for building detailed **system prompts** (with tool signatures) and user prompts (with history), handling API calls (supporting **visual input**), parsing JSON returned by LLMs (`thought`, `action`, `parameters`), and tracking token limits.
- `ReActAgent`: **Orchestrates the ReAct loop**, maintains execution step history (`ReActStep`). Its `run` method drives the entire process: call `LLMReasoner` to think -&amp;gt; record thought/action -&amp;gt; call `execute_tool` to execute action -&amp;gt; record observation result -&amp;gt; check if completed. `execute_tool` maps the action name returned by the LLM to a function in `tools.actions` and executes it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;cli&lt;/code&gt; Package:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Uses the `click` library to provide a command-line interface.
- The `run` command is the main entry point, responsible for parsing parameters, setting the **device serial number environment variable**, and launching the `ReActAgent`.
- Provides auxiliary commands for device management (`devices`, `connect`) and Portal application installation/setup (`setup`).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;ReAct Mechanism&lt;/p&gt;

&lt;p&gt;ReAct (Reasoning + Acting) is a &lt;strong&gt;core working mode&lt;/strong&gt; used by DroidRun to control Android devices. It allows AI to complete complex automation tasks by combining &lt;strong&gt;thinking&lt;/strong&gt; and &lt;strong&gt;action&lt;/strong&gt;, similar to how humans work.&lt;/p&gt;

&lt;p&gt;Core Idea: ReAct mimics the human problem-solving cycle.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reasoning:&lt;/strong&gt; AI (LLM) analyzes the task goal (like "open settings and enable dark mode"), combines it with the current screen state, and thinks about what needs to be done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Acting:&lt;/strong&gt; AI chooses a specific action (like "click the 'Display' button") and executes it on the Android device through tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observing:&lt;/strong&gt; AI sees the result after executing the action (like the screen jumping to the display settings page).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Think again...Act again...:&lt;/strong&gt; AI continues to think about what to do next based on the observed results, until the task is completed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ReAct Loop in DroidRun:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set Goal:&lt;/strong&gt; The user provides a natural language task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reason:&lt;/strong&gt; The LLM analyzes the task and current screen to decide on steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose Action:&lt;/strong&gt; The Agent selects an available action (such as click, input, swipe, analyze UI elements).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute:&lt;/strong&gt; Execute the action on the Android device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observe:&lt;/strong&gt; The Agent obtains the execution result (such as new screen content).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reason Again:&lt;/strong&gt; The Agent evaluates progress and decides on the next step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeat&lt;/strong&gt; until the task is completed or the maximum step limit is reached.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Key Features (in DroidRun):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Available Actions:&lt;/strong&gt; Include UI interaction (click, input), application management (launch, install), UI analysis (get elements), and task management (complete).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visual Capabilities (Optional):&lt;/strong&gt; When &lt;code&gt;vision=True&lt;/code&gt; is enabled, the Agent can analyze screenshots to better understand complex or non-standard UIs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Tracking:&lt;/strong&gt; Records Token consumption in LLM interactions for cost management and performance optimization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Recording:&lt;/strong&gt; The Agent records the type of each step (thinking, action, observation, etc.) for easy tracking and debugging.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's worth mentioning some details about the Agent package in the DroidRun Python library:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multi-LLM Support: The &lt;code&gt;LLMReasoner&lt;/code&gt; class encapsulates OpenAI, Anthropic, and Gemini API calls.&lt;/li&gt;
&lt;li&gt;Prompt Engineering: Carefully designed system prompts guide the LLM to analyze UI, think step by step, select tools, and return strict JSON.&lt;/li&gt;
&lt;li&gt;Multimodal Capabilities (Optional): Supports the &lt;code&gt;take_screenshot&lt;/code&gt; tool and passes image data to vision models (like GPT-4o, Claude 3). &lt;strong&gt;Screenshots serve as supplementary information&lt;/strong&gt; for handling scenarios that the accessibility API cannot cover (games, custom drawing, WebView internal details, disambiguation).&lt;/li&gt;
&lt;li&gt;History Recording and Truncation: Maintains ReAct step history and implements simple token budget truncation.&lt;/li&gt;
&lt;li&gt;Tool Abstraction: The LLM only needs to output tool names and parameters, and &lt;code&gt;ReActAgent&lt;/code&gt; is responsible for calling the specific implementations in &lt;code&gt;tools.actions&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Simply put, ReAct allows the AI agent to complete tasks on Android devices step by step, like a person with a brain who can act, observe, and reflect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Droidrun Portal (Android Application)
&lt;/h4&gt;

&lt;p&gt;This Android application is the &lt;strong&gt;perception layer (analogous to eyes and ears)&lt;/strong&gt; of the Droidrun system, with the main functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Core Mechanism:&lt;/strong&gt; Runs as an &lt;strong&gt;Accessibility Service&lt;/strong&gt;, using system APIs to &lt;strong&gt;read screen UI structure&lt;/strong&gt; without screenshots or root access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UI Scanning:&lt;/strong&gt; &lt;strong&gt;Periodically&lt;/strong&gt; (not responding in real-time to each event) scans the UI elements of the current active window (&lt;code&gt;AccessibilityNodeInfo&lt;/code&gt; tree).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Element Processing:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Extracts &lt;strong&gt;information&lt;/strong&gt; of visible elements (boundaries, text, class name, ID).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Builds parent-child hierarchy&lt;/strong&gt; based on spatial containment relationships.&lt;/li&gt;
&lt;li&gt;Assigns &lt;strong&gt;sequential indices (&lt;code&gt;clickableIndex&lt;/code&gt;)&lt;/strong&gt; to interactive/text elements for reference by the Python Agent.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Provision:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When receiving commands (such as &lt;code&gt;GET_ELEMENTS&lt;/code&gt;) sent by the Python Agent via &lt;strong&gt;ADB broadcast&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Serializes the processed UI element list (including hierarchy) into &lt;strong&gt;nested JSON&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Writes the JSON to a file&lt;/strong&gt; on the device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Point:&lt;/strong&gt; &lt;strong&gt;Prints (Logs) the path of the JSON file through Android Logcat&lt;/strong&gt;, informing the Python Agent where to get the data.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visualization (Optional):&lt;/strong&gt; Provides visual feedback through &lt;code&gt;OverlayManager&lt;/code&gt; by drawing rectangles with &lt;strong&gt;indices&lt;/strong&gt; and &lt;strong&gt;heat map colors&lt;/strong&gt; (based on detection time) as overlay layers on the screen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Model (&lt;code&gt;ElementNode.kt&lt;/code&gt;):&lt;/strong&gt; Defines the structure for storing UI element information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration:&lt;/strong&gt; Declares services and required permissions/capabilities through &lt;code&gt;AndroidManifest.xml&lt;/code&gt; and the accessibility service configuration file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Simply put, Droidrun Portal is an "assistant" running on an Android device that uses the accessibility service to "see" the content on the screen, organizes it into structured JSON data, and then tells the Python Agent where the data is hidden through an "agreement" (file path in Logcat). It can also optionally draw frames on the screen to show what it sees.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foundation of the AgentKit Vision: Cross-Platform Capabilities of AccessKit
&lt;/h2&gt;

&lt;p&gt;Droidrun appears (I haven't run it locally) to run smoothly on Android phones, but from a universality perspective, it has obvious limitations. It is precisely these limitations that inspired me to continue thinking about AgentKit, a universal cross-platform, cross-device AI automation kit.&lt;/p&gt;

&lt;p&gt;One of the huge challenges in implementing cross-platform AI automation is the &lt;strong&gt;vastly different accessibility APIs, UI frameworks, and development languages across platforms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Fortunately, in the Rust ecosystem, there is AccessKit, a &lt;strong&gt;cross-platform accessibility infrastructure library&lt;/strong&gt; based on Rust, designed to solve the above challenges and provide an ideal foundation for AgentKit.&lt;/p&gt;

&lt;p&gt;Its design inspiration partly comes from Chromium's multi-process accessibility architecture, adopting a &lt;strong&gt;push model&lt;/strong&gt;: UI toolkits actively push complete accessibility tree information and subsequent incremental updates to AccessKit's platform adapters, rather than waiting for adapters to pull on demand. This model is particularly suitable for toolkits that render UI elements themselves (including immediate mode GUI). Some of AgentKit's code (especially design concepts and data structures) also comes from Chromium.&lt;/p&gt;

&lt;p&gt;As a bridge between UI toolkits and native platform APIs, AccessKit's core abstractions are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Model:&lt;/strong&gt; Defines a set of data structures (nodes, roles, attributes, states, etc.) to describe the UI element hierarchy tree (Accessibility Tree). This model is platform-independent.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;common&lt;/code&gt; (accesskit crate):&lt;/strong&gt; Defines platform-independent UI tree data models (&lt;code&gt;Node&lt;/code&gt;, &lt;code&gt;Role&lt;/code&gt;, &lt;code&gt;Action&lt;/code&gt;, &lt;code&gt;TreeUpdate&lt;/code&gt;) and geometric types.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;consumer&lt;/code&gt; (accesskit_consumer crate):&lt;/strong&gt; Maintains complete tree states, handles updates, provides high-level APIs (traversal, query).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Push Model:&lt;/strong&gt; UI toolkits &lt;strong&gt;actively push&lt;/strong&gt; complete initial tree states and subsequent incremental &lt;code&gt;TreeUpdate&lt;/code&gt; to adapters. Adapters maintain internal states, directly responding to AT queries without frequent callbacks to applications. This is in contrast to the "pull model" of traditional AT APIs.

&lt;ul&gt;
&lt;li&gt;Advantages: Decoupling, good performance (especially for immediate mode GUI), asynchronous-friendly (default support for &lt;code&gt;async-io&lt;/code&gt; and providing &lt;code&gt;tokio&lt;/code&gt; optional features to adapt to different asynchronous runtimes).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Platform Adapters (&lt;code&gt;platforms/*&lt;/code&gt; crates):&lt;/strong&gt; Bridge the AccessKit model to native APIs (Windows UIA, macOS NSAccessibility, Unix AT-SPI, Android Accessibility Framework).&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Cross-Language Support:&lt;/strong&gt; The core library is implemented in Rust, bringing memory safety, high performance, zero-cost abstraction, concurrency safety, excellent FFI, and cross-platform compilation capabilities. It also provides bindings for languages like C and Python to facilitate integration with non-Rust toolkits.&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Pull Model vs Push Model&lt;/p&gt;

&lt;p&gt;Pull Model: This is a traditional or more direct approach. Imagine a screen reader (or other assistive technology, AT) wants to know the name of a button. It sends a request to your application through the platform's accessibility API: "Please tell me the name of the button with ID X." Your application (or UI toolkit) receives this request, looks up the corresponding information, and returns the name to the platform API, which eventually passes it to the screen reader. In this model, the information flow is &lt;strong&gt;actively pulled&lt;/strong&gt; by the AT or platform API.&lt;/p&gt;

&lt;p&gt;Push Model: AccessKit adopts this approach. Your application (or UI toolkit) doesn't wait for passive queries but &lt;strong&gt;actively pushes&lt;/strong&gt; the entire or partial state of the accessibility tree to AccessKit's platform adapter. When the UI changes, your application calculates the differences and pushes these changes to the adapter again. The platform adapter is responsible for maintaining a complete, up-to-date internal representation of the accessibility tree. When a screen reader queries information through the platform API (e.g., "What is the name of the node with ID Y?"), the platform adapter can look up and return the information directly from its own maintained tree state, usually without needing to go back and ask your application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AccessKit's core goal is to enable UI toolkits to provide necessary information to the operating system's accessibility APIs so that various assistive technologies (ATs) can understand and manipulate the UI. These assistive technologies include but are not limited to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Screen Readers:&lt;/strong&gt; Such as NVDA, JAWS on Windows, VoiceOver on macOS and iOS, TalkBack on Android, and Orca on Linux. They read aloud the text and UI element information on the screen, or output it to braille displays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Screen Magnifiers:&lt;/strong&gt; Such as Windows Magnifier, macOS Zoom. They magnify parts of the screen and may need to track focus or mouse position.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Voice Control Software:&lt;/strong&gt; Such as Dragon NaturallySpeaking, Windows Voice Access, macOS Voice Control. Users interact with the UI through voice commands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Switch Devices:&lt;/strong&gt; Used for users with severe mobility impairments, scanning and selecting UI elements through one or more switches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools for Reading and Writing Difficulties:&lt;/strong&gt; May need to highlight text, change fonts or colors, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AccessKit's platform adapters are responsible for effectively conveying this information to each platform's native APIs and assistive technologies, thereby simplifying the implementation of cross-platform accessibility.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;compared to Droidrun Portal&lt;/strong&gt;, both can provide structured UI perception capabilities, but AccessKit is cross-platform and provides a more general abstraction model, while Droidrun Portal is limited to Android.&lt;/p&gt;

&lt;p&gt;Therefore, it's clear that &lt;strong&gt;AccessKit's key to implementing universal AI automation control&lt;/strong&gt; is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Structured and Semantic UI Understanding:&lt;/strong&gt; Provides deeper interface understanding (roles, states, attributes, relationships) than screenshots.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standardized Interaction Interface:&lt;/strong&gt; Defines a unified &lt;code&gt;Action&lt;/code&gt; enumeration, providing a stable programming interface for simulating interactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Platform Consistency:&lt;/strong&gt; Core models and action definitions are consistent across platforms, facilitating the reuse of control logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For &lt;strong&gt;applications that have integrated AccessKit&lt;/strong&gt;, it provides an &lt;strong&gt;extremely ideal&lt;/strong&gt; foundation for perception and control. AI Agents can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Precise Perception:&lt;/strong&gt; Understand UI structure, element roles, states, and attributes by querying the AccessKit tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliable Execution:&lt;/strong&gt; Execute interactions by requesting standard AccessKit &lt;code&gt;Action&lt;/code&gt;s.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Platform Operation:&lt;/strong&gt; Reuse most control logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course, there is a &lt;strong&gt;prerequisite&lt;/strong&gt; here: the target application must integrate AccessKit. For applications that haven't integrated it, AI Agents still need to rely on other technologies (native APIs, visual automation, etc.).&lt;/p&gt;

&lt;p&gt;For &lt;strong&gt;applications built using AccessKit&lt;/strong&gt;, AccessKit provides an &lt;strong&gt;extremely powerful and ideal&lt;/strong&gt; infrastructure for AI Agents to perceive and control. It provides a structured, semantic, cross-platform, and relatively stable way to understand UI states and execute interactive actions, which has clear advantages over many existing automation technologies (especially visual automation).&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;its biggest limitation is the adoption rate of applications&lt;/strong&gt;. Currently, AccessKit is still a relatively new project, and there aren't many UI toolkits and applications that have adopted it. Therefore, a universal AI Agent aimed at controlling arbitrary desktop or mobile applications cannot widely rely on AccessKit yet and must have the capability to use other automation strategies. But as the AccessKit ecosystem develops, it has the potential to become a very valuable tool in the AI control field.&lt;/p&gt;

&lt;h2&gt;
  
  
  AgentKit: Universal AI Automation Framework Concept
&lt;/h2&gt;

&lt;p&gt;Based on the powerful foundation provided by AccessKit, we can envision a grander, more universal AI control framework—&lt;strong&gt;AgentKit&lt;/strong&gt;. AgentKit's goal is to become a modular, extensible AI automation solution for all platforms.&lt;/p&gt;

&lt;h3&gt;
  
  
  AgentKit Architecture Macro Overview
&lt;/h3&gt;

&lt;p&gt;AgentKit's design philosophy is layered, modular, and framework-agnostic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────┐
│                  AI Agent                   │
│  (LLM/Model, Task Planning, Decision Logic - Pluggable)  │
└──────────────────────┬──────────────────────┘
                       │ (Unified Instructions/States)
┌──────────────────────▼──────────────────────┐
│                 AgentKit Core               │
│ (State Management, Coordinator, Unified Action Model, Security Management)  │
└───┬───────────┬────────────────┬────────────┘
    │           │                │
┌───▼───┐   ┌───▼───┐      ┌────▼────┐
│AccessKit│  │ WebKit│      │Device  │
│ Bridge │  │ Bridge│      │ Bridge  │
└───┬───┘   └───┬───┘      └────┬────┘
    │           │                │
┌───▼───┐   ┌───▼───┐      ┌────▼────┐
│AccessKit│  │Browser/│      │ Platform │
│Adapters│  │WebView │      │Specific  │
└───┬───┘   └───┬───┘      └────┬────┘
    │           │                │
┌───▼───────────▼────────────────▼───┐
│        Applications &amp;amp; UIs          │
└───────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Architecture Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Three-Bridge Parallel Strategy:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AccessKit Bridge:&lt;/strong&gt; Used with priority, obtaining structured information and executing standard actions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebKit Bridge:&lt;/strong&gt; Handling Web content (browser/WebView), can execute JS. &lt;strong&gt;Solves WebView interaction challenges&lt;/strong&gt;: WebView exposes internal Web accessibility trees to the system (visible to AccessKit), but JS operations are more powerful and direct. This bridge provides JS execution capability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device Bridge:&lt;/strong&gt; Handling other non-phone smart terminal devices (Raspberry Pi, Jetson, Orange Pi, and other embedded Linux platforms).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coordinator Pattern:&lt;/strong&gt; The Core layer dynamically selects the most appropriate bridging method and handles mixed scenarios.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logical/Physical Separation:&lt;/strong&gt; Prioritizes semantics-based logical actions (&lt;code&gt;ClickElement&lt;/code&gt;), falling back to physical actions (&lt;code&gt;ClickPosition&lt;/code&gt;) when necessary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Data Exchange:&lt;/strong&gt; Drawing on Droidrun experience, but using shared memory/zero-copy memory/memory-mapped files + notification mechanism to replace the file system + Logcat, improving efficiency.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A core advantage of AgentKit's design will be supporting all UI frameworks that have implemented AccessKit, including but not limited to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Rust Native UI Frameworks&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Makepad: Hardware-accelerated cross-platform UI framework&lt;/li&gt;
&lt;li&gt;Druid: Data-driven Rust UI toolkit&lt;/li&gt;
&lt;li&gt;Iced: Simple cross-platform GUI library&lt;/li&gt;
&lt;li&gt;Vizia: Declarative Rust GUI library, focusing on audio applications&lt;/li&gt;
&lt;li&gt;Slint: Lightweight UI toolkit, suitable for embedded systems&lt;/li&gt;
&lt;li&gt;Bevy UI: Game UI system based on the Bevy engine&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Language UI Frameworks&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Flutter: Can integrate AccessKit through Rust bindings&lt;/li&gt;
&lt;li&gt;GTK: Supported through AccessKit adapters&lt;/li&gt;
&lt;li&gt;Qt: Can provide support through AccessKit bridging&lt;/li&gt;
&lt;li&gt;wxWidgets: Can integrate AccessKit to provide cross-platform accessibility support&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Experimental or Potentially Supported Frameworks in the Future&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Xilem: Experimental Rust GUI framework&lt;/li&gt;
&lt;li&gt;Egui: Immediate mode GUI library&lt;/li&gt;
&lt;li&gt;Custom rendering engines: Custom rendering systems used by games and professional applications&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For framework support, AgentKit adopts a plugin architecture, implementing flexible integration through adapter interfaces, fully leveraging the advantages of Rust language engineering capabilities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;dora-rs&lt;/code&gt; to Implement Automated Control of Non-Phone Smart Terminals
&lt;/h3&gt;

&lt;p&gt;Enabling AgentKit to control Raspberry Pi, Jetson, and other smart devices is &lt;strong&gt;completely feasible and extremely valuable extension direction&lt;/strong&gt;, which can expand AgentKit from GUI automation to a more universal AI control platform that interacts with the physical world. This corresponds to the Device Bridge part in the architecture above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Mechanism:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Device-Side Nodes:&lt;/strong&gt; Run &lt;code&gt;dora-rs&lt;/code&gt; nodes on target smart devices, acting as &lt;strong&gt;device drivers/proxies&lt;/strong&gt;, encapsulating interaction logic with specific hardware (GPIO, sensors, serial ports, etc.) or software (SDKs, APIs).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Central Brain:&lt;/strong&gt; AgentKit Core (running on PC/server) is responsible for decision-making.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Bridge:&lt;/strong&gt; AgentKit Core communicates with device-side &lt;code&gt;dora-rs&lt;/code&gt; nodes through the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standardized Data Flow:&lt;/strong&gt; Use &lt;code&gt;dora-rs&lt;/code&gt; message passing to standardize &lt;strong&gt;instructions&lt;/strong&gt; (such as &lt;code&gt;SetPinHigh&lt;/code&gt;, &lt;code&gt;ReadTemperature&lt;/code&gt;), &lt;strong&gt;states/data&lt;/strong&gt; (such as &lt;code&gt;PinState&lt;/code&gt;, &lt;code&gt;TemperatureReading&lt;/code&gt;), and &lt;strong&gt;capability descriptions&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Main Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Abstract Decoupling:&lt;/strong&gt; AgentKit Core doesn't need to care about device details, only interacting with standardized interfaces.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leveraging &lt;code&gt;dora-rs&lt;/code&gt; Advantages:&lt;/strong&gt; Fully utilizing its high performance, low latency, and data flow processing capabilities on embedded Linux.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy to Extend:&lt;/strong&gt; Adding new devices only requires developing corresponding &lt;code&gt;dora-rs&lt;/code&gt; nodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distributed Intelligence:&lt;/strong&gt; Can implement some local processing and control logic on the device side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unified Framework:&lt;/strong&gt; Use one set of AgentKit to orchestrate complex workflows involving GUI, Web, and physical devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An application example can be envisioned (Raspberry Pi temperature control LED):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Raspberry Pi runs &lt;code&gt;sensor_node&lt;/code&gt; (temperature and humidity reading node) and &lt;code&gt;led_node&lt;/code&gt; (LED control node).&lt;/li&gt;
&lt;li&gt;AgentKit Core (PC) instructs &lt;code&gt;sensor_node&lt;/code&gt; to read the temperature.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sensor_node&lt;/code&gt; reads and sends back &lt;code&gt;TemperatureReading(32.5)&lt;/code&gt; to Core.&lt;/li&gt;
&lt;li&gt;Core determines the temperature is over the limit (&amp;gt;30) and instructs &lt;code&gt;led_node&lt;/code&gt; to turn on the LED.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;led_node&lt;/code&gt; executes and returns &lt;code&gt;LedStatus(on)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this way, by running &lt;code&gt;dora-rs&lt;/code&gt; nodes on smart devices, AgentKit can reliably and efficiently extend its automation capabilities to the physical world, becoming a more powerful universal AI automation platform.&lt;/p&gt;

&lt;p&gt;AgentKit's design has significant advantages over platform-specific solutions (like Droidrun.ai):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Truly Cross-Platform&lt;/strong&gt;: Develop AI Agent logic once, and it can control applications on Windows, macOS, Linux, Android, iOS (through frameworks like Flutter), Web, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Framework Independence&lt;/strong&gt;: As long as the UI framework implements AccessKit (for native) or provides Web access interfaces, it can be controlled by AgentKit, allowing developers to freely choose their technology stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Reuse and Maintenance&lt;/strong&gt;: Core AI logic and control models only need to be maintained as one set, significantly reducing maintenance costs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unified User Experience&lt;/strong&gt;: Users can get consistent AI assistance or automation experiences across different devices and applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leveraging Native Capabilities&lt;/strong&gt;: Through AccessKit and Web APIs, AgentKit deeply leverages native platform capabilities, achieving precise, efficient perception and control, avoiding fragile image recognition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modularity and Extensibility&lt;/strong&gt;: Can easily add support for new frameworks, new platforms, or new AI models, ensuring the system can evolve with technological developments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Optimization&lt;/strong&gt;: Rust-based implementation provides excellent performance and memory safety guarantees, particularly suitable for real-time UI control scenarios.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  AgentKit Dedicated AI Gateway Service (Optional Enhancement)
&lt;/h3&gt;

&lt;p&gt;To better support cross-platform deployment, centralized management, and cloud inference capabilities, AgentKit can introduce a dedicated AI Gateway service as a bridge between clients and AI providers. This architecture is not necessary but can bring significant advantages.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Abstraction and Unification:&lt;/strong&gt; Provides clients with a single, stable API interface, shielding differences and changes in underlying AI providers (OpenAI, Anthropic, Gemini, etc.). Clients don't need to adapt to multiple API formats and authentication methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intelligent Routing and Load Balancing:&lt;/strong&gt; Automatically selects the most appropriate AI provider or model for inference based on request type (such as requiring visual capabilities), real-time load, cost, provider health status, and other factors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Enhancement:&lt;/strong&gt; Centrally manages API keys for all AI providers, eliminating the need for clients to directly store these sensitive credentials, reducing the risk of leakage. The Gateway is responsible for authenticating client identity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Optimization and Transformation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compression:&lt;/strong&gt; Intelligently compresses large data such as UI snapshots and history records, reducing network transmission volume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Format Conversion:&lt;/strong&gt; Converts AgentKit's internal request format to the format required by specific AI providers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context Management:&lt;/strong&gt; May intelligently truncate or summarize too-long history records.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response Caching:&lt;/strong&gt; Caches deterministic or high-frequency requests (e.g., requests with low &lt;code&gt;temperature&lt;/code&gt; settings) to avoid repeated calls to expensive AI APIs, reducing costs and latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring, Analysis, and Billing:&lt;/strong&gt; Centrally collects metrics such as API call count, Token consumption, latency, error rate, facilitating analysis of performance, cost, user behavior patterns, and enabling unified billing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting and Quota Management:&lt;/strong&gt; Implements unified rate limiting and quota management for clients to prevent abuse.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The AI Gateway system architecture design is roughly as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌───────────────────────┐
│   AgentKit Client     │
│  (Windows, macOS,     │
│   Linux, Mobile)      │
└───────────┬───────────┘
            │
            │ HTTPS / WebSockets (Encrypted Communication)
            │ (AgentKit Client &amp;lt;-&amp;gt; Gateway)
            ▼
┌───────────────────────────────────────────┐
│             AgentKit AI Gateway           │
│                                           │
│  ┌─────────────┐      ┌─────────────────┐ │
│  │ Authentication  │◀────▶│ Request Routing &amp;amp;  │ │
│  │ &amp;amp; Authorization │      │ Load Balancing  │ │
│  │ (Client Auth)│      └───────┬─────────┘ │
│  └──────┬──────┘              │           │
│         │                      │           │
│         ▼                      ▼           │
│  ┌─────────────┐      ┌─────────────────┐ │
│  │ Request Optimization  │──────▶│ Response Cache  │ │
│  │ &amp;amp; Transformation  │      └───────┬─────────┘ │
│  └──────┬──────┘              │           │
│         │                      │           │
│         ▼                      ▼           │
│  ┌─────────────────┐  ┌─────────────────┐ │
│  │ AI Provider I/F │  │ Monitoring &amp;amp;     │ │
│  │ (Unified Interface) │  │ Analysis      │ │
│  └──────┬──────┘      └─────────────────┘ │
└─────────┼─────────────────────────────────┘
          │ HTTPS (Gateway &amp;lt;-&amp;gt; AI Provider)
          │ (Including Provider API Key)
          ▼
    ┌────────────┐┌────────┐┌───────────────┐
    │ OpenAI API ││Claude  ││Other AI Providers │
    └────────────┘│  API   │└───────────────┘
                  └────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Core Component Implementation (Conceptual Explanation)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication and Authorization Module:&lt;/strong&gt; Verifies client API Key or Token, manages client permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Routing and Load Balancing:&lt;/strong&gt; Selects backend AI Provider based on rules (such as request characteristics, provider status, cost).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Optimization and Transformation:&lt;/strong&gt; Implements compression, format adaptation, context processing logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response Cache Management:&lt;/strong&gt; Uses Redis or memory cache to store cacheable responses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AI Provider Interface:&lt;/strong&gt; Defines a unified &lt;code&gt;trait AiProvider&lt;/code&gt;, and implements specific adapters for each provider, encapsulating their API calls and key management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and Analysis:&lt;/strong&gt; Integrates Prometheus/Grafana or similar systems to record key metrics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  AgentKit and Claude MCP / Google A2A Protocols Complementary Collaboration
&lt;/h2&gt;

&lt;p&gt;Recently, MCP and A2A, these two AI Agent-related protocols, have started to become popular. So I think it's necessary to compare them.&lt;/p&gt;

&lt;p&gt;First, let's clarify the core problems that each of these three technologies solves:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Technology&lt;/th&gt;
&lt;th&gt;Core Positioning&lt;/th&gt;
&lt;th&gt;Main Problem Solved&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;MCP&lt;/strong&gt; (Anthropic) Protocol&lt;/td&gt;
&lt;td&gt;Tool Connection Layer&lt;/td&gt;
&lt;td&gt;"How AI can call external tools and data sources"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;A2A&lt;/strong&gt; (Google) Protocol&lt;/td&gt;
&lt;td&gt;Agent Collaboration Layer&lt;/td&gt;
&lt;td&gt;"How different AI agents can securely communicate with each other"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;AgentKit&lt;/strong&gt; (Tool)&lt;/td&gt;
&lt;td&gt;UI Interaction Layer&lt;/td&gt;
&lt;td&gt;"How AI can perceive and operate user interfaces across platforms"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Relationship Analysis Between AgentKit and MCP
&lt;/h3&gt;

&lt;p&gt;MCP provides a standardized way to connect AI models with external tools, while AgentKit focuses on providing cross-platform UI control capabilities. This relationship is naturally complementary.&lt;/p&gt;

&lt;p&gt;MCP can handle "tool calls" (such as getting weather, searching for information, accessing files), while AgentKit handles "UI interactions" (such as clicking buttons, inputting text, parsing interface structures).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AgentKit can serve as an MCP tool provider&lt;/strong&gt;. AgentKit can be implemented as an MCP server, providing a standardized set of UI interaction tools. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example of AgentKit implemented as an MCP service
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;mcp.server.fastmcp&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastMCP&lt;/span&gt;

&lt;span class="c1"&gt;# Create MCP server
&lt;/span&gt;&lt;span class="n"&gt;mcp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastMCP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AgentKit UI Automation&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@mcp.tool&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;click_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Click a UI element with the specified ID.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="c1"&gt;# AgentKit internal implementation of cross-platform clicking
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;agent_kit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bridges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coordinator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;AgentAction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ClickElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;element_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The serious security challenges that MCP currently faces (such as tool poisoning attacks) also provide important warnings for AgentKit:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Tool Isolation&lt;/strong&gt;: AgentKit needs to implement a sandbox-like mechanism to limit the permission scope of UI interaction operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear Permission Model&lt;/strong&gt;: AgentKit should adopt fine-grained permission control, distinguishing between reading UI (low risk) and executing operations (high risk)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secondary Confirmation Mechanism&lt;/strong&gt;: Key operations (such as submitting forms, deleting content) should require explicit user confirmation&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Relationship Analysis Between AgentKit and A2A
&lt;/h3&gt;

&lt;p&gt;The A2A protocol focuses on collaboration and communication between AI Agents, while AgentKit focuses on UI interaction capabilities, which are also highly complementary. AgentKit can be implemented as an A2A-compatible agent, dedicated to handling UI interaction tasks, thereby enhancing AI Agent division of labor and collaboration. A2A's security mechanisms (such as AgentCard, authentication) can also enhance AgentKit's security.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Example&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;AgentKit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;an&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;A&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;A&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;agent's&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;AgentCard&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"UI Interaction Agent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Specialized in cross-platform user interface interaction"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"provider"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"organization"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"AgentKit.org"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"skills"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ui_discovery"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"UI Element Discovery"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Discover and analyze interactive elements on the interface"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ui_interaction"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"UI Element Interaction"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Interact with interface elements (click, input, etc.)"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"authentication"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"schemes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"bearer"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;AgentCard: A public metadata file in the A2A protocol (usually located at &lt;code&gt;/.well-known/agent.json&lt;/code&gt;), describing the agent's capabilities, skills, endpoint URLs, and authentication requirements. Clients use this file for discovery.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Three-Layer Architecture Integration Scheme
&lt;/h3&gt;

&lt;p&gt;Based on the above analysis, a three-layer architecture integrating MCP, A2A, and AgentKit can be constructed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│              A2A Collaboration Layer    │
│  (Agent Coordination, Task Assignment, Secure Communication)  │
└───────────────────┬─────────────────────┘
                    │
┌───────────────────▼─────────────────────┐
│              MCP Tool Layer             │
│  (Standardized Tool Calls, Resource Access)  │
└─────────┬─────────────────────┬─────────┘
          │                     │
┌─────────▼────────┐   ┌────────▼─────────┐
│   AgentKit UI Layer  │   │  Other Specialized Tools  │
│  (Cross-platform UI Interaction)  │   │  (Search, Calculation, etc.)  │
└──────────────────┘   └──────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this architecture:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;A2A Layer&lt;/strong&gt; handles task assignment and collaboration between agents&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;MCP Layer&lt;/strong&gt; provides a standardized tool calling interface&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;AgentKit Layer&lt;/strong&gt; serves as a specialized tool for MCP, providing cross-platform UI interaction capabilities&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Above is my technical vision for AgentKit, a universal AI automation framework (or kit).&lt;/p&gt;

&lt;p&gt;We are in an era where AI technology is profoundly influencing human-computer interaction. I foresee that future applications will not be completely replaced by AI, but will form a &lt;strong&gt;diversified interaction ecosystem&lt;/strong&gt;: AI agents, traditional graphical applications, and hybrid systems that combine both will coexist.&lt;/p&gt;

&lt;p&gt;User experience will also become &lt;strong&gt;layered&lt;/strong&gt;: at the high level, users can conveniently express their intentions through natural language; at the low level, AI is responsible for translating intentions into specific operations, and when necessary, calling appropriate graphical interfaces for users to perform fine control or receive intuitive feedback.&lt;/p&gt;

&lt;p&gt;This evolutionary trend highlights the need for new infrastructure. My AgentKit concept is &lt;strong&gt;deeply inspired by Droidrun.ai's exploration of automation on the Android platform&lt;/strong&gt;. Droidrun.ai's practice verifies the feasibility of AI-driven device control but also exposes its single-platform limitations.&lt;/p&gt;

&lt;p&gt;Therefore, the core goal of AgentKit is to &lt;strong&gt;transcend platform silos and achieve truly unified cross-platform automation&lt;/strong&gt;. The key technical support for this vision is &lt;strong&gt;AccessKit&lt;/strong&gt;. It provides a standardized, cross-platform accessibility interface that allows AI Agents to "understand" and "operate" user interfaces under different operating systems and application frameworks in a consistent way.&lt;/p&gt;

&lt;p&gt;Through this technical vision of AgentKit, I see a clear possible path for AI-driven automation to move from platform silos to cross-platform unification. Rust's safety, performance, and cross-platform capabilities, combined with AccessKit's unified accessibility interface and the understanding capabilities of modern LLMs, together provide a solid technical foundation for this vision.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>agents</category>
      <category>accesskit</category>
    </item>
    <item>
      <title>Moly: An Open-Source LLM Client Implemented in Pure Rust</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Wed, 01 Jan 2025 17:16:19 +0000</pubDate>
      <link>https://dev.to/zhanghandong/moly-an-open-source-llm-client-implemented-in-pure-rust-1hmd</link>
      <guid>https://dev.to/zhanghandong/moly-an-open-source-llm-client-implemented-in-pure-rust-1hmd</guid>
      <description>&lt;p&gt;Moly is an AI LLM client written in Rust. As a flagship application project of Robius, it demonstrates the powerful development capabilities of Makepad UI and Robius.&lt;br&gt;
Currently, Moly supports Mac/Linux/Windows platforms, and will support iOS and Android mobile platforms in the near future.&lt;br&gt;
You can follow the instructions at &lt;code&gt;https://github.com/moxin-org/moly&lt;/code&gt; to install and use Moly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Moly's Basic Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Discovering Models
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0wr8lzddxbmddeywqcw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0wr8lzddxbmddeywqcw.png" alt="Image description" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The initial interface displays a list of locally supported Featured Agents and Models. Note that if your version doesn't support Agents yet, you won't see Featured Agents content.&lt;br&gt;
You can choose to download the open-source model that suits your machine configuration. Click the arrow in the bottom right corner of the window to expand and view the current model's download status. You can also pause or delete models that are being downloaded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5s65qwbbsci9227r1iz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5s65qwbbsci9227r1iz8.png" alt="Image description" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you scroll down the window page, a Sort By dropdown list will appear in the top right corner of the window. You can sort the model list according to your needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fxdwx7rqbe6wgkvczwy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fxdwx7rqbe6wgkvczwy.png" alt="Image description" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also directly search for the model you want through the search bar at the top of the window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzby7zb9jovz2cs6mwysh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzby7zb9jovz2cs6mwysh.png" alt="Image description" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Managing Downloaded Models
&lt;/h3&gt;

&lt;p&gt;You can click the My Models Tab in the left operation bar to manage your downloaded models.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvtig62uv0k9uibysvmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvtig62uv0k9uibysvmz.png" alt="Image description" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the 'i' icon in each row of the model list to view detailed information about the model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl1ofe9cmptdkwd9iut0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl1ofe9cmptdkwd9iut0.png" alt="Image description" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click 'Show in Finder' to view the model download directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqz289s0fgaiu2mzyt68h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqz289s0fgaiu2mzyt68h.png" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click 'Change Download location' to modify the download directory path.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93uyja7vxv1b7cjjqmec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93uyja7vxv1b7cjjqmec.png" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Chatting with AI
&lt;/h3&gt;

&lt;p&gt;Click the Chat Tab in the sidebar to bring up the chat window. Click the arrow button at the top of the window to select your currently downloaded large model.&lt;br&gt;
Of course, you can also open this window by clicking the 'Chat with Model' button in the local large model list managed in the My Models Tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8yymxzo9fp0hmu8ik42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8yymxzo9fp0hmu8ik42.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the chat input box at the bottom of the window, you can directly ask questions to your chosen large model.&lt;br&gt;
You can also use the '@' operation to bring up a selection box to choose the large model or Agent you want to converse with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct6gtni6klqntfqrqs3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct6gtni6klqntfqrqs3c.png" alt="Image description" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you can have a conversation with it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno5v1s1cgxv9jljzo22i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno5v1s1cgxv9jljzo22i.png" alt="Image description" width="800" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the collapse icon in the top left corner of the window to hide the 'CHATS' chat window list on the left. Click again to expand it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsz7mqn5svpdafxi50m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsz7mqn5svpdafxi50m5.png" alt="Image description" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, clicking the arrow collapse button in the top right corner of the window will bring up the 'Chat Setting' window, where you can configure the large model's System Prompt and parameters according to your needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F234fixb5khz4q1x5sjeg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F234fixb5khz4q1x5sjeg.png" alt="Image description" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  System Configuration
&lt;/h3&gt;

&lt;p&gt;Click the 'Setting' button in the bottom left corner of the sidebar to open Moly's system configuration window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8duz5njl2m0hy6tjh0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8duz5njl2m0hy6tjh0m.png" alt="Image description" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, you can modify the backend large model service port. You can also add a new MoFA Agent service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0ndie2yt3cglgjm4w3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0ndie2yt3cglgjm4w3p.png" alt="Image description" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Moly Drives Makepad's Evolution
&lt;/h2&gt;

&lt;p&gt;As Robius's flagship application, Moly's role is also to promote the evolution of the Makepad UI framework. Let's use the evolution of two Makepad basic Widgets as examples to illustrate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modal Widget
&lt;/h3&gt;

&lt;p&gt;Earliest discussions (early October):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Jmbejar proposed a new Modal design approach - "Modal deref" mode, inspired by practical experience from the Moly project. The main advantages are:

&lt;ul&gt;
&lt;li&gt;Modal content can be implemented as an independent &lt;code&gt;widget&lt;/code&gt; rather than as a Modal child component&lt;/li&gt;
&lt;li&gt;Simplifies how developers implement complex modal content without needing to consider parent-child component communication&lt;/li&gt;
&lt;li&gt;Follows the &lt;code&gt;deref&lt;/code&gt; pattern commonly used in &lt;code&gt;makepad&lt;/code&gt; View widget&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Kevin raised some considerations about this design:

&lt;ul&gt;
&lt;li&gt;The current Modal design only requires developers to correctly handle dismissed action&lt;/li&gt;
&lt;li&gt;The new design requires developers to implement more interfaces (open, close, capture actions, etc.)&lt;/li&gt;
&lt;li&gt;Need to balance whether it increases the burden on novice developers&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Problem evolution (mid-October to November):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Some visual issues were discovered in actual use:

&lt;ul&gt;
&lt;li&gt;Modal background content "leakage"&lt;/li&gt;
&lt;li&gt;Random text highlighting&lt;/li&gt;
&lt;li&gt;Issues particularly when using Modal + Dock combination&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Latest improvement direction (mid-November):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Julian proposed the &lt;code&gt;draw_depth&lt;/code&gt; propagation solution:

&lt;ul&gt;
&lt;li&gt;Pass &lt;code&gt;draw_depth&lt;/code&gt; to child components through Scope&lt;/li&gt;
&lt;li&gt;Child components use larger &lt;code&gt;draw_depth&lt;/code&gt; values when drawing&lt;/li&gt;
&lt;li&gt;This approach may solve visual overlay issues&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In summary, Moly project's improvements to Makepad Modal widget are mainly reflected in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Architecture design level: Provided the "Modal deref" design approach, making component structure clearer&lt;/li&gt;
&lt;li&gt;Practical verification: Discovered and solved problems in actual applications through use in Moly&lt;/li&gt;
&lt;li&gt;Development experience: Promoted discussion of developer friendliness, balancing flexibility and ease of use&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This case demonstrates how Moly, as a Makepad practical project, drove improvements and evolution of the underlying widget system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sliding Panels Widget
&lt;/h3&gt;

&lt;p&gt;Initial state and requirements of the sliding panels component:&lt;/p&gt;

&lt;p&gt;Makepad already had a basic ExpandablePanel component (widgets/expandable_panel.rs), which was actually used in the Wonderous project's &lt;code&gt;timeline_screen.rs&lt;/code&gt;. This laid the foundation for subsequent sliding panel development.&lt;/p&gt;

&lt;p&gt;A key turning point in evolution emerged when developers proposed a new use case: Kevin wanted to implement an overlay panel that slides in from the right side to display user profile information. This requirement had several important characteristics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The panel needs to be presented as an overlay layer&lt;/li&gt;
&lt;li&gt;Sliding in should not change the size of the main content area&lt;/li&gt;
&lt;li&gt;This functionality needs to combine features from Moly project's FadeView and Modal&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This requirement prompted the development team to rethink the sliding panel design.&lt;/p&gt;

&lt;p&gt;It's particularly noteworthy that this evolution process was significantly influenced by the Moly project. Moly's FadeView and Modal components provided important reference implementations for Makepad, demonstrating how to combine animation effects and overlay layers. This again shows how Moly, as a practical project, drives the advancement of the Makepad widget system.&lt;/p&gt;

&lt;p&gt;Overall, the evolution of sliding panels reflects the maturation process of the Makepad UI framework driven by practical applications - developing from basic functional components into a complete, unified interaction design language. In this process, the Moly project played a crucial catalytic role, guiding Makepad's development direction through providing practical experience and mature implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Development
&lt;/h2&gt;

&lt;p&gt;Moly has currently added Agent support in the dev branch. This Agent capability is obtained through integration with &lt;a href="https://github.com/moxin-org/mofa" rel="noopener noreferrer"&gt;MoFA&lt;/a&gt;.&lt;br&gt;
If you're interested, you can follow the configuration described &lt;a href="https://gist.github.com/joulei/fb95b4b90afba3e359d169451733323d" rel="noopener noreferrer"&gt;here&lt;/a&gt; to run Moly and MoFA to try the Agent features shown earlier.&lt;br&gt;
Moly will add more Agent tools through MoFA in the future.&lt;br&gt;
Additionally, Moly is considering deep integration with &lt;a href="https://github.com/moxin-org/Moxin-LLM" rel="noopener noreferrer"&gt;Moxin LLM&lt;/a&gt;. Moxin LLM is a truly open-source large model that fully complies with the &lt;a href="https://arxiv.org/pdf/2403.13784" rel="noopener noreferrer"&gt;Model Openness Framework (MOF)&lt;/a&gt;.&lt;br&gt;
Finally, Moly will also support iOS and Android mobile platforms.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>llm</category>
      <category>moly</category>
      <category>moxin</category>
    </item>
    <item>
      <title>Learning Rust You Need a Cognitive Frame</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Fri, 08 Apr 2022 03:55:32 +0000</pubDate>
      <link>https://dev.to/zhanghandong/learning-rust-you-need-a-cognitive-frame-41oa</link>
      <guid>https://dev.to/zhanghandong/learning-rust-you-need-a-cognitive-frame-41oa</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;If learning Rust feels hard, painful or confusing, you might want to read this article.  But if you feel comfortable learning Rust, then this article is useless to you and can be ignored.&lt;/p&gt;

&lt;p&gt;Although there is a wealth of Rust learning materials available, Rust is a relatively expensive language to learn. The cost of learning varies slightly for people with different language experience. &lt;/p&gt;

&lt;p&gt;Although there is a wealth of Rust learning materials available, Rust is a language with a relatively high learning curve.The cost of learning varies slightly for people with different language experience.&lt;/p&gt;

&lt;p&gt;Before learning Rust, You need to know what the mental cost of learning Rust will be.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;People with zero foundation&lt;/strong&gt; need to make up some necessary computer science related fundamentals as well as build an understanding of programming. Beginning to learn programming is a big challenge in itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;People who have some basic programming skills&lt;/strong&gt; can be a bit of a hindrance to learning Rust, although it can be helpful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;People with only C experience&lt;/strong&gt; have the following challenges when learning Rust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rust programming paradigm. The &lt;code&gt;C&lt;/code&gt; language is procedural, while &lt;code&gt;Rust&lt;/code&gt; is a hybrid programming paradigm that supports both object-oriented and functional programming styles. It is easy for someone coming from C to write Rust in a procedural style, and while it is possible to write full functionality in Rust in a procedural style, the advantages of Rust are lost in the code architecture. Concepts related to programming paradigms: &lt;code&gt;generics&lt;/code&gt; &lt;code&gt;/ trait&lt;/code&gt; / &lt;code&gt;error handling&lt;/code&gt; /&lt;code&gt;structs&lt;/code&gt; / &lt;code&gt;enums&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ownership and borrowing checks. &lt;code&gt;C&lt;/code&gt; manages memory manually, but &lt;code&gt;Rust&lt;/code&gt; manages it with ownership. &lt;code&gt;C&lt;/code&gt; uses pointers, but in Rust, pointers are abstracted safely to references, requiring borrow checks. These are the concepts that &lt;code&gt;C&lt;/code&gt; developers must master.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Unsafe Rust&lt;/code&gt; safe abstraction. &lt;code&gt;C&lt;/code&gt; developers need to understand the &lt;code&gt;Unsafe Rust&lt;/code&gt;coding specification and how to do safe abstraction, which is important, especially when calling each other with C. &lt;/li&gt;
&lt;li&gt;Macros. Rust declaration macros similar to the C language declaration macros, are code replacement, but more powerful than C, these should also be considered a learning challenge. rust there are more powerful procedural macros, you can learn in the early stages of learning without considering, and then learn later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;People with Cpp experience&lt;/strong&gt;, who by default have some knowledge of C, have the following challenges when learning Rust.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rust programming paradigm. The hybrid paradigm of Rust is different from  Cpp. Rust is not a pure object-oriented language and does not have constructors. the generics, traits, enumerations, and error handling in Rust are also important for Cpp developers to learn. &lt;/li&gt;
&lt;li&gt;Ownership and borrowing checks. Because Rust also introduces the same RAII-based smart pointers as Cpp, memory management is easier to understand for those with experience in Cpp 11 and above. However, Cpp developers with no experience in this area will have some difficulty.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Unsafe Rust&lt;/code&gt; safe abstraction. This is the same as for beginners from C. It requires some understanding of the Unsafe Rust coding specification.&lt;/li&gt;
&lt;li&gt;Generic and Procedural Macros. Cpp developers have templates, but Rust has generics, which are not as powerful as Cpp templates, but have procedural macros to compensate. That is, Cpp template programming is equivalent to a combination of Rust's generics and procedural macros. If Cpp developers want to pursue the effects of templating in Rust, then they need to master generics and procedural macros.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;People with only experience in GC languages&lt;/strong&gt;, such as Java, Python, Ruby, Haskell, etc., face essentially the same challenges in learning Rust as Cpp, but the learning curve is much steeper because most people using the GC language do not have a deep enough understanding of the underlying memory management.&lt;/p&gt;

&lt;p&gt;Thus, the steepness of the learning curve for Rust has to do not only with the complexity of the language itself, but also with each individual's programming base.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust Cognitive Frame
&lt;/h2&gt;

&lt;p&gt;Once you realize the root cause of the Rust learning curve, you will find that your previous programming knowledge does not transfer smoothly to learning the Rust language.&lt;/p&gt;

&lt;p&gt;So, you need a general learning framework that you can follow to create a learning plan to combat this learning curve and get started with Rust and keep learning.&lt;/p&gt;

&lt;p&gt;This learning frame is simple, and requires only two things to understand.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, don't be in a hurry, keep a beginner's mindset, and treat it in stages. &lt;/li&gt;
&lt;li&gt;Second, at each stage of the learning process, do not just have input, but also maintain the output. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is said to be a learning frame, but it is actually a cognitive frame. Next, let's look at the specifics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Rust in stages
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;For those who have some basic programming skills&lt;/strong&gt;, there are at least three stages to learning Rust.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Learn the Rust syntax as a whole&lt;/strong&gt;. Get a good understanding of the Rust syntax and language features. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rust basic ownership concepts need some deep understanding&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep domain learning&lt;/strong&gt;. After the above two phases are completed, you can put the domain into practice and learn it further in depth.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Stage I:  Comprehensive Understanding of the Rust Syntax
&lt;/h3&gt;

&lt;p&gt;The goal of this first phase is to gain a comprehensive understanding of the Rust syntax.&lt;/p&gt;

&lt;p&gt;What do you need to do?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gain a more comprehensive understanding of the Rust syntax. &lt;/li&gt;
&lt;li&gt;Classify the Rust syntax.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This phase is not for you to learn Rust in one go, so don't get too bogged down in what you don't understand, and allow yourself to keep a list of questions if you don't understand it for a while. The focus of this phase is to gain a comprehensive understanding of the Rust syntax and build a syntax structure in your mind, with a focus on categorization. The syntax should be sorted into categories. For example, data types, control flow, structures, traits and generics, macros, etc., and what each of them does.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recommended Study Materials for Stage I
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://doc.rust-lang.org/book/#the-rust-programming-language"&gt;The Rust Programming Language&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://doc.rust-lang.org/cargo/"&gt;The Rust Cargo Book&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Others,  free or paid introductory Rust materials.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When looking at these materials, you should focus on the first stage of learning: a comprehensive understanding of the Rust syntax.&lt;/p&gt;

&lt;p&gt;Once you have a basic understanding of the Rust syntax, you can test yourself with the official &lt;a href="https://doc.rust-lang.org/rust-by-example/"&gt;Rust by example&lt;/a&gt; and &lt;a href="https://github.com/rust-lang/"&gt;Rustlings&lt;/a&gt; to test the results of your learning.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recommended practice projects for stage II
&lt;/h4&gt;

&lt;p&gt;There are some misconceptions about the choice of first-stage practice projects, and many people like to use Rust to solve algorithmic problems to learn Rust. But in fact, the learning effect is not good.&lt;/p&gt;

&lt;p&gt;Because of Rust ownership restrictions, it is not as flexible and free as other languages for implementing some algorithms and data structures. If you don't have a deep understanding of Rust ownership, it's easier to give up on Rust when you encounter difficulties in solving problems, and it's not easy to stick with it.&lt;/p&gt;

&lt;p&gt;So at this stage, it is better to do projects that give you a sense of accomplishment. The general principle is: combine your own experience in your domain, choose simple projects to start.&lt;/p&gt;

&lt;p&gt;Here are some recommended practice projects.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Combine the official &lt;a href="https://rust-cli.github.io/book/index.html"&gt;Command Line Apps in Rust&lt;/a&gt; book to implement a simple terminal application, such as reading a CSV file or something.&lt;/li&gt;
&lt;li&gt;Go to the GitHub open source repository for case inspiration.

&lt;ol&gt;
&lt;li&gt;for example, implement a terminal typing practice application like &lt;a href="https://github.com/Samyak2/toipe"&gt;toipe&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;or a simple editor like &lt;a href="https://github.com/file-acomplaint/kyun"&gt;kyun &lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Others.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Stage II: Mastering Key Concepts of the Rust Language
&lt;/h3&gt;

&lt;p&gt;Once you have achieved the learning goals of stage 1, you are ready to begin stage 2.&lt;/p&gt;

&lt;p&gt;The goal of stage 2 is to master the key concepts of the Rust language. The following concepts are covered: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Ownership and borrowing checks. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Type systems and programming paradigms. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unsafe Rust and macros.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The focus is on Ownership and Borrowing checks. Only after mastering these two concepts can you get started with the Rust language. The other concepts can be mastered gradually as you work on projects, but you can't be ignorant of them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recommended Study Materials for Stage II
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nostarch.com/rust-rustaceans"&gt;《Rust for Rustaceans》&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oreilly.com/library/view/programming-rust-2nd/9781492052586/"&gt;《Programming Rust, Second Edition》&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="https://rust-unofficial.github.io/patterns/"&gt;Rust Design Patterns&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Rust Standard Library Documentation&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rust-unofficial.github.io/too-many-lists/"&gt;Too Many lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;other advanced Rust books, and free or paid learning materials.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is not necessary to read all of the materials recommended here. you can focus on "Ownership and borrowing checks" in depth. then go through the concepts of "Type Systems and Programming Paradigms", "Macros", and "Unsafe Rust", and also dive into the concepts of Rust design patterns. After that, take the time to read the Rust standard library documentation in depth to understand the Rust coding specification.&lt;/p&gt;

&lt;p&gt;After the above, you'll have a good foundation to put Rust into production practice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recommended practice projects for stage II
&lt;/h4&gt;

&lt;p&gt;Combine your experience in your domain of expertise and choose simple projects, starting from the simple to the deep.&lt;/p&gt;

&lt;p&gt;At the same time, you can also start reading the source code of some of the better-known projects to start learning.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At this point, you can read 《Too Many Lists》 to implement a linked list, or you can solve LeetCode to test your understanding of the Rust ownership mechanism.&lt;/li&gt;
&lt;li&gt;GitHub project for inspiration, for example:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/servo/rust-smallvec"&gt;smallvec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/erikgrinaker/toydb"&gt;toydb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Others. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stage III : Deep into the real-world domain
&lt;/h3&gt;

&lt;p&gt;After the above two stages of learning, learners can commit to their own real-world projects and start learning. If they do not have their own real projects, they can participate in the open source project contributions.&lt;/p&gt;

&lt;p&gt;This process is a long-term process of learning and applying Rust. For example, Rust concurrency and asynchronous development-related content, key concepts about type systems, programming paradigms, macros, and in-depth learning and application of Unsafe Rust are all done in this stage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping up with input and output
&lt;/h2&gt;

&lt;p&gt;The learning process is about maintaining a balance between input and output.&lt;/p&gt;

&lt;p&gt;What do I mean by input? Drawing various knowledge from various learning resources is called input.&lt;/p&gt;

&lt;p&gt;Output can take many forms. Writing an article, participating in a project, or doing a sharing are all outputs.&lt;/p&gt;

&lt;p&gt;Only when input and output are kept in a cycle will the human brain be in thinking mode and what you input will be precipitated into structured memory.&lt;/p&gt;

&lt;p&gt;So, in addition to input, you have to keep yourself in output during the above learning phase. That way your energy and time will not be wasted.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Two diagrams showing the current Rust network ecosystem</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Tue, 10 Aug 2021 04:37:16 +0000</pubDate>
      <link>https://dev.to/zhanghandong/two-diagrams-showing-the-current-rust-network-ecosystem-39hi</link>
      <guid>https://dev.to/zhanghandong/two-diagrams-showing-the-current-rust-network-ecosystem-39hi</guid>
      <description>&lt;p&gt;Today I drew two diagrams to show the current Rust Web ecosystem.&lt;/p&gt;

&lt;h2&gt;
  
  
  tokio ecosystem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filqnj15eq4sl2jmd42uf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filqnj15eq4sl2jmd42uf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the diagram, the tokio ecosystem is now complete with the basic core components for web services and web development. Especially with the introduction of the Axum framework, tokio is close to being complete in the Web ecosystem.&lt;/p&gt;

&lt;p&gt;Axum's middleware uses the tower abstraction directly, which has the following advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the use of a unified Service and Layer abstraction standards, to facilitate everyone to prosper the ecology&lt;/li&gt;
&lt;li&gt;reuse the tokio / hyper / tonic ecosystem&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;axum's routing mechanism does not use property macros like rocket, but provides a simple DSL (chain call). Routing is based on iterations and regular expressions to match, and routing performance should be similar to actix-web.&lt;/p&gt;

&lt;p&gt;A convenient extractor is also provided, as long as FromRequest is implemented it is an extractor and very easy to implement.&lt;/p&gt;

&lt;p&gt;There are also some others advantages.&lt;/p&gt;

&lt;p&gt;In a word, Axum is, in my opinion, a milestone for Rust in the Web development field, and it strongly drives the tokio/tower ecosystem. It's not quite mature yet, but it has a lot of potential.&lt;/p&gt;

&lt;h2&gt;
  
  
  Others Web Frameworks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdu0qjqitrxvunpy9r0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdu0qjqitrxvunpy9r0f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;actix-web&lt;/code&gt; itself adds a layer of runtime threads as Actor to manage multiple threads, each thread is actually running a tokio single thread &lt;code&gt;block_on&lt;/code&gt; , so that between the threads can not task stealing , lost the advantages of tokio task scheduling , in exchange for the performance of wireless thread context switching . This is the main difference between actix-web and other frameworks. actix-web's middleware also borrows from Tower Service, but it is not as generic as tower.&lt;/p&gt;

&lt;p&gt;The advantage of &lt;code&gt;rocket&lt;/code&gt; is a well-developed API, especially for handling forms very well. Unlike other frameworks, rocket has strong constraints on middleware in order to achieve security and correct goals, and it is not as free to implement middleware as Axum. This also predestines it to be more difficult to form an generic ecosystem. rocket is not performance oriented at the moment, maybe it will be optimized for performance in the future after 1.0.&lt;/p&gt;

&lt;p&gt;How to choose your own web framework, combined with your scenario and preferences to choose it.&lt;/p&gt;

</description>
      <category>tokio</category>
      <category>rust</category>
    </item>
    <item>
      <title>Rust Concept Clarification: Deref vs AsRef vs Borrow vs Cow</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Tue, 06 Jul 2021 12:28:53 +0000</pubDate>
      <link>https://dev.to/zhanghandong/rust-concept-clarification-deref-vs-asref-vs-borrow-vs-cow-13g6</link>
      <guid>https://dev.to/zhanghandong/rust-concept-clarification-deref-vs-asref-vs-borrow-vs-cow-13g6</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2251koglfie808mxji2j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2251koglfie808mxji2j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By HanDong Zhang&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding by Module
&lt;/h2&gt;

&lt;p&gt;In fact, the classification by standard library will first give you a small idea of what they do. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/ops/index.html" rel="noopener noreferrer"&gt;ops&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/ops/trait.Deref.html" rel="noopener noreferrer"&gt;Deref&lt;/a&gt; . As you can see, &lt;code&gt;Deref&lt;/code&gt; is categorized as an &lt;code&gt;ops&lt;/code&gt; module. If you look at the documentation, you will see that this module defines the  &lt;a href="https://doc.rust-lang.org/std/ops/index.html#traits" rel="noopener noreferrer"&gt;trait&lt;/a&gt;  fr all the overloadable operators. For example, &lt;code&gt;Add trait&lt;/code&gt; corresponds to &lt;code&gt;+&lt;/code&gt;, while &lt;code&gt;Deref trait&lt;/code&gt;  corresponds to a shared(immutable) borrowing dereference operation, such as &lt;code&gt;*v&lt;/code&gt; . Correspondingly, there is also the &lt;code&gt;DerefMut trait&lt;/code&gt;， which corresponds to the dereferencing operation of exclusive(mutable) borrowing. Since the Rust ownership semantics is a language feature throughout , the semantics of &lt;code&gt;Owner&lt;/code&gt; / &lt;code&gt;immutable borrowing(&amp;amp;T)&lt;/code&gt;/ &lt;code&gt;mutable borrowing(&amp;amp;mut T)&lt;/code&gt; all appear together.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/convert/index.html" rel="noopener noreferrer"&gt;convert&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/convert/trait.AsRef.html" rel="noopener noreferrer"&gt;AsRef&lt;/a&gt; . As you can see, &lt;code&gt;AsRef&lt;/code&gt; is grouped under the convert module. if you look at the documentation, you will see that  &lt;a href="https://doc.rust-lang.org/std/convert/index.html#traits" rel="noopener noreferrer"&gt;traits&lt;/a&gt; related to type conversions are defined in this module. For example, the familiar "From/To", "TryFrom/TryTo" and "AsRef/AsMut" also appear in pairs here, indicating that the feature is releated to type conversions. Based on the naming rules in the  &lt;a href="https://rust-lang.github.io/api-guidelines/" rel="noopener noreferrer"&gt;Rust API Guidelines&lt;/a&gt; , wen can infer that methods starting with &lt;code&gt;as_&lt;/code&gt; represent conversions from &lt;code&gt;borrow -&amp;gt; borrow&lt;/code&gt;, i.e, &lt;code&gt;reference -&amp;gt; reference&lt;/code&gt; , and are overhead-free. And such conversions do not fail.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/index.html" rel="noopener noreferrer"&gt;borrow&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/trait.Borrow.html" rel="noopener noreferrer"&gt;Borrow&lt;/a&gt;. As you can see, &lt;code&gt;Borrow&lt;/code&gt; is categorized in the borrow module. The documentation for this module is very minimal, with a single sentence saying that this is for using borrowed data. So the trait is more or less related to expressing borrwo semantics. Three  &lt;a href="https://doc.rust-lang.org/std/borrow/index.html#traits" rel="noopener noreferrer"&gt;traits&lt;/a&gt;  are provided:  &lt;a href="https://doc.rust-lang.org/std/borrow/trait.Borrow.html" rel="noopener noreferrer"&gt;Borrow&lt;/a&gt; / &lt;a href="https://doc.rust-lang.org/std/borrow/trait.BorrowMut.html" rel="noopener noreferrer"&gt;BorrowMut&lt;/a&gt;/ &lt;a href="https://doc.rust-lang.org/std/borrow/trait.ToOwned.html" rel="noopener noreferrer"&gt;ToOwned&lt;/a&gt; , which corresponds exactly to the ownership semantics.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/index.html" rel="noopener noreferrer"&gt;borrow&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/enum.Cow.html" rel="noopener noreferrer"&gt;Cow&lt;/a&gt;. It can be seen that &lt;code&gt;Cow&lt;/code&gt; is also classified as a borrow module.  According to the description, &lt;code&gt;Cow&lt;/code&gt; is a clone-on-write smart pointer. The main reason for putting it in the borrow module is to use borrowing as much as possible and avoid copying, as an optimization.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/ops/index.html" rel="noopener noreferrer"&gt;ops&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/ops/trait.Deref.html" rel="noopener noreferrer"&gt;Deref&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First, let's look at the definition of the trait.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Deref&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;#[must_use]&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The definition is not complicated, &lt;code&gt;Deref&lt;/code&gt; contains only a &lt;code&gt;deref&lt;/code&gt; method signature. The beauty of this trait is that it is called "implicitly" by the compiler, officially called "&lt;a href="https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion" rel="noopener noreferrer"&gt;deref coercion&lt;/a&gt; ". &lt;/p&gt;

&lt;p&gt;Here is an example from the standard library.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;ops&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Deref&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;DerefExample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Deref&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;DerefExample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Target&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DerefExample&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code, the &lt;code&gt;DerefExample&lt;/code&gt; structure implements the &lt;code&gt;Deref&lt;/code&gt; trait, so it can be executed using the dereference operator &lt;code&gt;*&lt;/code&gt;. In the example, the value of the field value is returned directly.&lt;/p&gt;

&lt;p&gt;As you can see, &lt;code&gt;DerefExample&lt;/code&gt; has a pointer-like behavior , because it implements &lt;code&gt;Deref&lt;/code&gt;, because it can be dereferenced. &lt;code&gt;DerefExample&lt;/code&gt; also becomes a kind of smart pointer. This is one way to identify if a type is a smart pointer, by seeing if it implements &lt;code&gt;Deref&lt;/code&gt;. But not all smart pointers implement &lt;code&gt;Deref&lt;/code&gt;, some implent &lt;code&gt;Drop&lt;/code&gt;, or both.&lt;/p&gt;

&lt;p&gt;Now let's summarize &lt;code&gt;Deref&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;T&lt;/code&gt; implements &lt;code&gt;Deref&amp;lt;Target=U&amp;gt;&lt;/code&gt;, and &lt;code&gt;x&lt;/code&gt; is an instance of type &lt;code&gt;T&lt;/code&gt;, then.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In an immutable context, the operation of &lt;code&gt;*x&lt;/code&gt; (when &lt;code&gt;T&lt;/code&gt; is neither a reference nor a primitive pointer) is equivalent to &lt;code&gt;*Deref::deref(&amp;amp;x)&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The value of &lt;code&gt;&amp;amp;T&lt;/code&gt; is forced to be converted to the value of &lt;code&gt;&amp;amp;U&lt;/code&gt;. (deref coercion).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;T&lt;/code&gt; implements all the (immutable) methods of &lt;code&gt;U&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The beauty of &lt;code&gt;Deref&lt;/code&gt; is that it enhances the Rust development experience. A typical example from the standard library is that &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; shares all the methods of &lt;code&gt;slice&lt;/code&gt; by implemented  &lt;code&gt;Deref&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Allocator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;ops&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Deref&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_raw_parts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For example, the simplest method, &lt;code&gt;len()&lt;/code&gt;, is actually defined in the &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html" rel="noopener noreferrer"&gt; &lt;code&gt;slice&lt;/code&gt; &lt;/a&gt; module. In Rust, when executing &lt;code&gt;.&lt;/code&gt; call, or at the function argument position, the compiler automatically performs the implicit act of deref coercion. so it is equivalent to &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; having the &lt;code&gt;slice&lt;/code&gt; method as well.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 当 a 调用 len() 的时候，发生 deref 强转&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Implicit behavior in Rust is not common,  but &lt;code&gt;Deref&lt;/code&gt; is one of them, and its implicit coercion make smart pointers easy to use.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="nf"&gt;.to_uppercase&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"HELLO"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For example, if we manipulate &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt; , instead of manually dereferencing &lt;code&gt;T&lt;/code&gt; inside to manipulate it,  as if the outer layer of &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt; is transparent, we can manipulate T directly.&lt;/p&gt;

&lt;p&gt;Another example.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.to_uppercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;"HELLO"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The argument type of the &lt;code&gt;uppercase&lt;/code&gt; method above is obviously &lt;code&gt;&amp;amp;str&lt;/code&gt;, but the actual type passed in the main function is &lt;code&gt;&amp;amp;String&lt;/code&gt;, so why does it compile successfully? It is because &lt;code&gt;String&lt;/code&gt; implements&lt;code&gt;Deref&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nn"&gt;ops&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Deref&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;#[inline]&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;str&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_utf8_unchecked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's the beauty of &lt;code&gt;Deref&lt;/code&gt;. But some people may mistake it for inheritance. Big mistake.&lt;/p&gt;

&lt;p&gt;This behavior seems a bit like inheritance, but please don't just use &lt;code&gt;Deref&lt;/code&gt; to simulate inheritance.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/convert/index.html" rel="noopener noreferrer"&gt;convert&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/convert/trait.AsRef.html" rel="noopener noreferrer"&gt;AsRef&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Let's look at the definition of &lt;code&gt;AsRef&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;as_ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We already know that &lt;code&gt;AsRef&lt;/code&gt; can be used for conversions. Compared to &lt;code&gt;Deref&lt;/code&gt;, which has an implicit behavior, &lt;code&gt;AsRef&lt;/code&gt; is an explicit conversion.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;is_hello&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;is_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;is_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, the function of &lt;code&gt;is_hello&lt;/code&gt; is a generic function. The conversion is achieved by qualifying &lt;code&gt;T: AsRef&amp;lt;str&amp;gt;&lt;/code&gt; and using an explicit call like &lt;code&gt;s.as_ref()&lt;/code&gt; inside the function. Either &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;str&lt;/code&gt; actually implements the &lt;code&gt;AsRef&lt;/code&gt; trait.&lt;/p&gt;

&lt;p&gt;So now the question is, when do you use &lt;code&gt;AsRef&lt;/code&gt;? Why not just use &lt;code&gt;&amp;amp;T&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Consider an example like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Thing&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Thing&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;WhatTypeHere&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Thing&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="nf"&gt;.some_conversion&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, the &lt;code&gt;new&lt;/code&gt; function name has the following options for the type parameter.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;str&lt;/code&gt;. In this case, the caller needs to pass in a reference. But in order to convert to String, the called party (callee) needs to control its own memory allocation, and will have a copy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt;. In this case, the caller is fine passing String, but if it is passing a reference, it is similar to case 1. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;T: Into&amp;lt;String&amp;gt;&lt;/code&gt;. In this case, the caller can pass &lt;code&gt;&amp;amp;str&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;, but there will be memory allocation and copying during the type conversion as well.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;T: AsRef&amp;lt;str&amp;gt;&lt;/code&gt;. Same as case 3. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;T: Into&amp;lt;Cow&amp;lt;'a, str&amp;gt;&amp;gt;&lt;/code&gt;, where some allocations can be avoided. &lt;code&gt;Cow&lt;/code&gt; will be described later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is no one-size-fits-all answer to the question of when to use which type. Some people just like &lt;code&gt;&amp;amp;str&lt;/code&gt; and will use it no matter what. There are trade-offs here.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On occasions when assignment and copying are less important,  there is no need to make type signatures too complicated, just use &lt;code&gt;&amp;amp;str&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Some need to look at method definitions and whether they need to consume ownership, or return ownership or borrowing. &lt;/li&gt;
&lt;li&gt;Some need to minimize assignment and copy, so it is necessary to use more complex type signatures, as in case 5.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Application of Deref and AsRef in API design
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;a href="https://github.com/rustwasm/wasm-bindgen" rel="noopener noreferrer"&gt;wasm-bindgen&lt;/a&gt;&lt;/strong&gt; library contains a component called &lt;a href="https://github.com/rustwasm/wasm-bindgen/tree/" rel="noopener noreferrer"&gt;&lt;strong&gt;web-sys&lt;/strong&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This component is the binding of Rust to the browser Web API. As such, web-sys makes it possible to manipulate the browser DOM with Rust code, fetch server data, draw graphics, handle audio and video, handle client-side storage, and more.&lt;/p&gt;

&lt;p&gt;However, binding Web APIs with Rust is not that simple. For example, manipulating the DOM relies on JavaScript class inheritance, so web-sys must provide access to this inheritance hierarchy. In web-sys, access to this inheritance structure is provided using &lt;code&gt;Deref&lt;/code&gt; and &lt;code&gt;AsRef&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Deref&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="nf"&gt;.append_child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// call a method on `Node`&lt;/span&gt;

&lt;span class="nf"&gt;method_expecting_a_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// coerce to `&amp;amp;Node` implicitly&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// explicitly coerce to `&amp;amp;Node`&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you have &lt;code&gt;web_sys::Element&lt;/code&gt;, then you can get &lt;code&gt;web_sys::Node&lt;/code&gt; implicitly by using deref.&lt;/p&gt;

&lt;p&gt;The use of deref is mainly for API ergonomic reasons, to make it easy for developers to use the &lt;code&gt;.&lt;/code&gt; operation to transparently use the parent class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using AsRef&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A large number of &lt;code&gt;AsRef&lt;/code&gt; conversions are also implemented in web-sys for various types.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;HtmlElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;EventTarget&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;JsValue&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;HtmlAnchorElement&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A reference to a parent structure can be obtained by explicitly calling &lt;code&gt;.as_ref()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Deref focuses on implicitly and transparently using the parent structure, while AsRef focuses on explicitly obtaining a reference to the parent structure. This is a trade-off with a specific API design, rather than a mindless simulation of OOP inheritance.&lt;/p&gt;

&lt;p&gt;Another example of using AsRef is the &lt;a href="https://github.com/http-rs/http-types" rel="noopener noreferrer"&gt;http-types&lt;/a&gt; library, which uses AsRef and AsMut to convert various types.&lt;/p&gt;

&lt;p&gt;For example, Request is a combination of &lt;code&gt;Stream / headers/ URL&lt;/code&gt;, so it implements &lt;code&gt;AsRef&amp;lt;Url&amp;gt;&lt;/code&gt;, &lt;code&gt;AsRef&amp;lt;Headers&amp;gt;&lt;/code&gt;, and &lt;code&gt;AsyncRead&lt;/code&gt;. Similarly, Response is a combination of &lt;code&gt;Stream / headers/ Status Code&lt;/code&gt;. So it implements &lt;code&gt;AsRef&amp;lt;StatusCode&amp;gt;&lt;/code&gt;, &lt;code&gt;AsRef&amp;lt;Headers&amp;gt;&lt;/code&gt;, and &lt;code&gt;AsyncRead&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;forwarded_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nn"&gt;http_types&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Headers&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// get the X-forwarded-for header&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 所以，forwarded_for 可以方便处理 Request/ Response / Trailers &lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;fwd1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;forwarded_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;fwd2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;forwarded_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;fwd3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;forwarded_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;trailers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/index.html" rel="noopener noreferrer"&gt;borrow&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/trait.Borrow.html" rel="noopener noreferrer"&gt;Borrow&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Take a look at the definition of &lt;code&gt;Borrow&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Borrow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Borrowed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;borrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Borrowed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Contrast &lt;code&gt;AsRef&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;as_ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Isn't this very similar? So, some people suggest that one of these two functions could be removed altogether. But in fact, there is a difference between Borrow and AsRef, and they both have their own uses.&lt;/p&gt;

&lt;p&gt;The Borrow trait is used to represent borrowed data. the AsRef trait is used for type conversion. In Rust, it is common to provide different type representations for different use cases for different semantics.&lt;/p&gt;

&lt;p&gt;A type provides a reference/borrow to &lt;code&gt;T&lt;/code&gt; in the &lt;code&gt;borrow()&lt;/code&gt; method by implementing &lt;code&gt;Borrow&amp;lt;T&amp;gt;&lt;/code&gt;, expressing the semantics that it can be borrowed, rather than converted to some type &lt;code&gt;T&lt;/code&gt;. A type can be freely borrowed as several different types, or it can be borrowed in a mutable way.&lt;/p&gt;

&lt;p&gt;So how do you choose between Borrow and AsRef?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose Borrow when you want to abstract different borrow types in a uniform way, or when you want to create a data structure that handles self-contained values (owned) and borrowed values (borrowed) in the same way.&lt;/li&gt;
&lt;li&gt;When you want to convert a type directly to a reference and you are writing generic code, choose AsRef. simpler case.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact, the HashMap example given in the standard library documentation explains this very well. Let me translate it for you.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;HashMap&amp;lt;K, V&amp;gt;&lt;/code&gt; stores key-value pairs, and its API should be able to retrieve the corresponding value in the HashMap properly using either the key's own value or its reference. Since the HashMap has to hash and compare keys, it must require that both the key's own value and the reference behave the same when hashed and compared.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;borrow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Borrow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// fields omitted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The insert method uses the key's own value and takes ownership of it.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Hash&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Eq&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// If you use the get method to get the corresponding value by key, you can use the reference of key, which is denoted by &amp;amp;Q here&lt;/span&gt;
    &lt;span class="c1"&gt;// and requires Q to satisfy `Q: Hash + Eq + ?Sized`&lt;/span&gt;
    &lt;span class="c1"&gt;// As for K, it is expressed as a borrowed data of Q by `K: Borrow&amp;lt;Q&amp;gt;`.&lt;/span&gt;
    &lt;span class="c1"&gt;// So, the hash implementation of Q is required to be the same as K&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;where&lt;/span&gt;
        &lt;span class="n"&gt;K&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Borrow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Hash&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Eq&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Borrow is a bound on borrowed data and is used with additional traits, such as &lt;code&gt;Hash&lt;/code&gt; and &lt;code&gt;Eq&lt;/code&gt; in the example.&lt;/p&gt;

&lt;p&gt;See another example.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="c1"&gt;//  Can this structure be used as the key of a HashMap?&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;CaseInsensitiveString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// It implements PartialEq without problems&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;PartialEq&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;CaseInsensitiveString&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Note that the comparison here is required to ignore ascii case&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="nf"&gt;.eq_ignore_ascii_case&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;Eq&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;CaseInsensitiveString&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Implementing Hash is no problem&lt;/span&gt;
&lt;span class="c1"&gt;// But since PartialEq ignores case, the hash calculation must also ignore case&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Hash&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;CaseInsensitiveString&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Hasher&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="nf"&gt;.to_ascii_lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Can CaseInsensitiveString implement &lt;code&gt;Borrow&amp;lt;str&amp;gt;&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Obviously, CaseInsensitiveString and str have different implementations of Hash. str does not ignore case. Therefore, &lt;code&gt;Borrow&amp;lt;str&amp;gt;&lt;/code&gt; must not be implemented for CaseInsensitiveString, so CaseInsensitiveString cannot be used as a key for a HashMap. What happens if we force &lt;code&gt;Borrow&amp;lt;str&amp;gt;&lt;/code&gt; to be used? It will fail due to case difference when determining the key.&lt;/p&gt;

&lt;p&gt;But CaseInsensitiveString can be fully implemented as AsRef.&lt;/p&gt;

&lt;p&gt;This is the difference between Borrow and AsRef. &lt;code&gt;Borrow&lt;/code&gt; is a bit stricter and represents a completely different semantics than &lt;code&gt;AsRef&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/std/index.html" rel="noopener noreferrer"&gt;std&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/index.html" rel="noopener noreferrer"&gt;borrow&lt;/a&gt;::&lt;a href="https://doc.rust-lang.org/std/borrow/enum.Cow.html" rel="noopener noreferrer"&gt;Cow&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Look at the definition of &lt;code&gt;Cow&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Cow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;ToOwned&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nb"&gt;Sized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
 &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Borrowed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Owned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;ToOwned&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Owned&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, Cow is an enumeration. It is somewhat similar to Option, in that it represents one of two cases, Cow here means borrowed and self-owned, but only one of these cases can occur.&lt;/p&gt;

&lt;p&gt;The main functions of Cow are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;acts as a smart pointer, providing transparent immutable access to instances of this type (e.g. the original immutable methods of this type can be called directly, implementing Deref, but not DerefMut).&lt;/li&gt;
&lt;li&gt;if there is a need to modify an instance of this type, or to gain ownership of an instance of this type, &lt;code&gt;Cow&lt;/code&gt; provides methods to do cloning and avoid repeated cloning.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;Cow&lt;/code&gt; is designed to improve performance (reduce replication) while increasing flexibility, because most of the time, business scenarios are read more and write less. With &lt;code&gt;Cow&lt;/code&gt;, this can be achieved in a uniform, canonical form, where object replication is done only once when a write is needed. This may reduce the number of replications significantly.&lt;/p&gt;

&lt;p&gt;It has the following key points to master.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Cow&amp;lt;T&amp;gt;&lt;/code&gt; can directly call the immutable methods of &lt;code&gt;T&lt;/code&gt;, since &lt;code&gt;Cow&lt;/code&gt;, an enumeration, implements &lt;code&gt;Deref&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;.to_mut()&lt;/code&gt; method can be used to obtain a mutable borrow with an ownership value when &lt;code&gt;T&lt;/code&gt; needs to be modified.

&lt;ol&gt;
&lt;li&gt;note that a call to &lt;code&gt;.to_mut()&lt;/code&gt; does not necessarily result in a Clone.&lt;/li&gt;
&lt;li&gt;calling &lt;code&gt;.to_mut()&lt;/code&gt; when ownership is already present is valid, but does not produce a new Clone.&lt;/li&gt;
&lt;li&gt;multiple calls to &lt;code&gt;.to_mut()&lt;/code&gt; will produce only one Clone.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;.into_owned()&lt;/code&gt; can be used to create a new owned object when &lt;code&gt;T&lt;/code&gt; needs to be modified, a process that often implies a memory copy and the creation of a new object.

&lt;ol&gt;
&lt;li&gt;calling this operation will perform a Clone if the value in the previous &lt;code&gt;Cow&lt;/code&gt; was in borrowed state.&lt;/li&gt;
&lt;li&gt;this method, whose argument is of type &lt;code&gt;self&lt;/code&gt;, will "consume" the original instance of that type, after which the life cycle of the original instance of that type will end, and cannot be called more than once on &lt;code&gt;Cow&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Cow is used more often in API design.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;borrow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Use Cow for the return value to avoid multiple copies&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;remove_spaces&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Cow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with_capacity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;.chars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Owned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;Cow&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Borrowed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Of course, when to use Cow comes back to the "when to use &lt;code&gt;AsRef&lt;/code&gt;" discussion in our previous article, there are trade-offs and no one-size-fits-all standard answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;To understand the various types and traits in Rust, you need to take into account the ownership semantics and ponder the documentation and examples, which should be easy to understand. I don't know if reading this article has solved your doubts? Feel free to share your feedback.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
  </channel>
</rss>
