<?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: Zhangwuji</title>
    <description>The latest articles on DEV Community by Zhangwuji (@zhangwuji).</description>
    <link>https://dev.to/zhangwuji</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%2F1359349%2Fafe14344-d720-4592-b61f-2abc9eed2655.png</url>
      <title>DEV Community: Zhangwuji</title>
      <link>https://dev.to/zhangwuji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhangwuji"/>
    <language>en</language>
    <item>
      <title>rust lifeCycle</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 29 May 2024 08:02:20 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-lifecycle-557c</link>
      <guid>https://dev.to/zhangwuji/rust-lifecycle-557c</guid>
      <description>&lt;p&gt;what is lifeCycle?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = String::from("ss");
let b;

{
  let c=String::from("test");
  b=&amp;amp;c;

}

print!("{}",b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp;c will not live long enough；&lt;br&gt;
you can solve this problem&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    let mut b;

    {
        let c=String::from("test");
        b=&amp;amp;c;

    };
    let c2=String::from("test");
    b=&amp;amp;c2;
    print!("{}",b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because &amp;amp;c will not be given the control' itself to b; so &amp;amp;c will be destroied; b now is null;if this situation in other language, will throw null pointer error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    let mut b;

    {
        let c=String::from("testfffffffffgggg");
        // let g=Box::new(c);
        b=c;

    };

    print!("{}",b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now the control of c is given to b; the memory of c in head will not be destroied。&lt;/p&gt;

&lt;p&gt;1-------------&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn test(&amp;amp;'a a,&amp;amp;'b b) -&amp;gt; &amp;amp;'a {
   retun b
}

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

&lt;/div&gt;



&lt;p&gt;&amp;amp;'a &amp;amp;'b indicate different lifecycle&lt;/p&gt;

&lt;p&gt;2-------------------&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn test2(&amp;amp;a a) -&amp;gt; &amp;amp;a{

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

&lt;/div&gt;



&lt;p&gt;&amp;amp;a equal &amp;amp;'a a if the parameter only have one. this 'a could be omited &lt;/p&gt;

&lt;p&gt;3---------------------------&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub struct NewArticle&amp;lt;T&amp;gt; {
    pub headline: String,
    pub content: String,
    pub location: T,
}

impl&amp;lt;T&amp;gt; NewArticle &amp;lt;T&amp;gt; {
    fn summarize(&amp;amp;self,ff:T) -&amp;gt; String {
        //   let content= self.content;
        //   return  content;
        return format!("{}", self.content);
    }


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

&lt;/div&gt;



&lt;p&gt;&amp;amp;self euqal &amp;amp;'a self&lt;/p&gt;

&lt;p&gt;how to use life cycle in Gernerics？&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub struct A &amp;lt;'a&amp;gt;{
    name:&amp;amp;'a 


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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  struct A&amp;lt;'a, T&amp;gt; {
        a: &amp;amp;'a T,
        b: &amp;amp;'a T,
    };
    impl&amp;lt;'a, T&amp;gt; A&amp;lt;'a, T&amp;gt; {
        fn new(one: &amp;amp;'a T, two: &amp;amp;'a T) -&amp;gt; Self {
            A { a: one, b: two }
        }

        fn first(self) -&amp;gt; &amp;amp;'a T {
            self.a
        }

        fn update(self){
            // self.a.push
        }
    }

    let b = A::new(&amp;amp;[1, 23], &amp;amp;[1, 23]);
let c = A::new(&amp;amp;1, &amp;amp;2);
    println!("{:#?}",b.first());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;1 is i32 type. it staies in stack, &amp;amp;1 is reference of i32. it also staies in stack。&lt;/p&gt;

</description>
    </item>
    <item>
      <title>rust trait</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 04:10:54 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-trait-4n1i</link>
      <guid>https://dev.to/zhangwuji/rust-trait-4n1i</guid>
      <description>&lt;p&gt;you can regard trait as interface as ts.but it is more flexiable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub struct Animal {
   name:String
}
pub trait Action {
    fn say() ;
    fn getName -&amp;gt; String；
}
impl  Action for Animal {
     fn say(){

      },
    fn getName() -&amp;gt;String{

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

&lt;/div&gt;



&lt;p&gt;and you can set default implement for tait&lt;br&gt;
such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub trait Action {
   fn say(){

      },
    fn getName -&amp;gt; String；
}

impl  Action for Animal {

    fn getName() -&amp;gt;String{

    } 
}

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

&lt;/div&gt;



&lt;p&gt;seond powerful function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub trait Action&amp;lt;T,U&amp;gt; {
   fn say(){

      },
    fn getName -&amp;gt; String；
}

pub trait2 Action&amp;lt;T,U&amp;gt; {
   fn say(){

      },
    fn getName -&amp;gt; String；
}

fn main(ff:impl trait&amp;lt;String,String&amp;gt; + trait2&amp;lt;String,String&amp;gt; ) {

}

fn main&amp;lt;T:trait&amp;lt;String,String&amp;gt; + trait2&amp;lt;String,String&amp;gt;&amp;gt;(ff:T){

}

fn main&amp;lt;T,U&amp;gt;(ff:T)
where
   T:trait&amp;lt;String,String&amp;gt; + trait2&amp;lt;String,String&amp;gt;
{

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

&lt;/div&gt;



&lt;p&gt;the three methods are equal&lt;/p&gt;

</description>
    </item>
    <item>
      <title>rust Generics</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 03:01:35 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-generics-lpa</link>
      <guid>https://dev.to/zhangwuji/rust-generics-lpa</guid>
      <description>&lt;p&gt;what is Generics？&lt;br&gt;
the essence of Generics is assuming it to be certain type。&lt;br&gt;
the true type is not know until excution&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;how to use generics in function？
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn &amp;lt;T,U&amp;gt;name(st:T):U{

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;how to use generics in stuct？&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stuct A &amp;lt;T,U&amp;gt; {
  string:T
  age:U
}

impl &amp;lt;T,U&amp;gt; A &amp;lt;T,U&amp;gt; {
     fn say&amp;lt;T2&amp;gt;(&amp;amp;self,param:T2) -&amp;gt; T2 {

        return  param;
    }
} 


`3. how to use generics in enum？``



enum Result &amp;lt;T,U&amp;gt;{
   Ok(T),
   Err(U)

}






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

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Rust type Result</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 02:48:16 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-type-result-150p</link>
      <guid>https://dev.to/zhangwuji/rust-type-result-150p</guid>
      <description>&lt;ol&gt;
&lt;li&gt;
type Result is enum
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pub enum Result&amp;lt;T, E&amp;gt; {
    /// Contains the success value
    #[lang = "Ok"]
    #[stable(feature = "rust1", since = "1.0.0")]
    Ok(#[stable(feature = "rust1", since = "1.0.0")] T),

    /// Contains the error value
    #[lang = "Err"]
    #[stable(feature = "rust1", since = "1.0.0")]
    Err(#[stable(feature = "rust1", since = "1.0.0")] E),
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let me create a function that returntype is Result&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn test(path:String)-&amp;gt;Result&amp;lt;&amp;amp;str,&amp;amp;str&amp;gt; {
      if path == "d" {
            Ok("我是成功了")
       }else{
           Error("我失败了")
       }

}

match test {
    Ok(s) =&amp;gt;{

    },
    Error(x)=&amp;gt;{

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Rust release</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 02:23:20 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-release-11i8</link>
      <guid>https://dev.to/zhangwuji/rust-release-11i8</guid>
      <description>&lt;p&gt;setup cargo.toml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[profile.dev]
panic = 'abort'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you called cargo run &lt;br&gt;
if your code happen panic!(); rust excutor dose not expand stack，Just exit main thread. let OS solve the error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[profile.release]
panic = 'abort'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you called cargo run  --release&lt;br&gt;
if your code happen panic!(); rust excutor dose not expand stack，Just exit main thread. let OS solve the error.&lt;/p&gt;

&lt;p&gt;--release mean product environment；the build time would be slow, but packaged file will small.  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>cmd and powershell</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 01:50:36 +0000</pubDate>
      <link>https://dev.to/zhangwuji/cmd-and-powershell-369a</link>
      <guid>https://dev.to/zhangwuji/cmd-and-powershell-369a</guid>
      <description>&lt;p&gt;in windows os&lt;br&gt;
how to set cmd environment variable？&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set fff = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;how to set  environment variable in powershell?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$env:RUST_BACKTRACE = "1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;how to output all  environment variable into console in powershell?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gci Env:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;how to output all  environment variable into console in cmd?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the mothods bellow just make environment variable live in one powershell or cmd ？ when you close it. they will be destroied。&lt;/p&gt;

</description>
    </item>
    <item>
      <title>rust hashMap</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Wed, 22 May 2024 01:29:21 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-hashmap-1dp7</link>
      <guid>https://dev.to/zhangwuji/rust-hashmap-1dp7</guid>
      <description>&lt;ol&gt;
&lt;li&gt;how to create hashmap in rust？
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use std::collections::HashMap;
let a = HashMap::new();

let a2= HashMap::from([
("key","value"),
("key","value")
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;how to insert ("key","value") into map instance?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a2= HashMap::from([
("key","value"),
("key","value")
]);
a2.insert("dd","22")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.how to remove someone out of map instance?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; map.remove(&amp;amp;String::from("color2")); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.how to update value of key?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     map.entry("test2").or_insert(200);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.how to get value of key?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let val=  map.get("test2");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;how to traverse map instance?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut a2= HashMap::from([
("key","value"),
("key","value")
]);

for (key,value) in &amp;amp;mut a2{
   value*="213"
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Unicode ASCII the history of character set</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Tue, 21 May 2024 15:37:54 +0000</pubDate>
      <link>https://dev.to/zhangwuji/unicode-ascii-the-history-of-character-set-4j8m</link>
      <guid>https://dev.to/zhangwuji/unicode-ascii-the-history-of-character-set-4j8m</guid>
      <description>&lt;p&gt;1.&lt;br&gt;
one byte have eight bit;one bit is 0 or 1. is basical unit;&lt;/p&gt;

&lt;p&gt;computer developed in Us。So one bytes could stand for all alphabet of english and common character。they make a character set called ASCll, one character corresponded to one byte。&lt;br&gt;
对应&lt;br&gt;
with computer developping more and more popular，more countries lauange wanted to be joined;china make character set call GBK that chinese character occupied 2 byte.the situation is so complicated and unmananged becauseof many contries has own character set。 in order to &lt;br&gt;
solve this problem apple company invented unicode table initially。&lt;/p&gt;

&lt;p&gt;in order to compatible ASCll and the charactor of ASCll occupied one bytes；&lt;/p&gt;

&lt;p&gt;But how to clearly know how many bytes  one character corresponds  to？&lt;br&gt;
they invented utf-8 encode and decode plan。&lt;br&gt;
it specified  In a byte if the leading bytes are 111.it indicates three bytes sequence。&lt;br&gt;
if one bytes&lt;br&gt;
0 ....&lt;br&gt;
if two bytes&lt;br&gt;
110 ......  10.....&lt;/p&gt;

&lt;p&gt;if three bytes&lt;br&gt;
110 ......  10..... 10 ....&lt;br&gt;
... correspond true code of unicode set&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Rust ： String and chart set and code type</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Tue, 21 May 2024 14:54:58 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-string-and-chart-set-and-code-type-34di</link>
      <guid>https://dev.to/zhangwuji/rust-string-and-chart-set-and-code-type-34di</guid>
      <description>&lt;p&gt;1.&lt;br&gt;
how to splice multiple string types？&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let s1 = String::from("Hello");
 let s2 = String::from("World");
 let s3 = s1 + &amp;amp;s2;

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

&lt;/div&gt;



&lt;p&gt;s1 will be destroied in this way。 So this way is not recommonded。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  let s1 = String::from("Hello");
  let s2 = String::from("World");
  let s3 = s1.clone() + &amp;amp;s2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this way is so complicated，let us see it's simple way;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        let mut s1 = String::from("Hello");
        let s2 = String::from("World");
        let s3 = format!("========={}{}", s1, s2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.&lt;br&gt;
 rust string use  unicode character set，and the rule of encode and &lt;br&gt;
 decode useing utf-8, So the string's every char may be occupy &lt;br&gt;
 different numer of bytes。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     let s1 = String::from("नमस्ते");
     println!("{}",s1.len())

       let s1 = String::from("智");
       println!("{}",s1.len()) ;

      let s1 = String::from("123");
       println!("{}",s1.len()) ;

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

&lt;/div&gt;



&lt;p&gt;you will find different char; they occupy different number of bytes。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let s1 = String::from("नमस्ते");
let s2= &amp;amp;s1[2..5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;remember  this index is in the string splice'process is bytes index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let start_byte = s.char_indices().nth(start).map(|(i, _)| i).unwrap_or(s.len());

let end_bytes = s.char_indices().nth(start).map(|(i, _)| i).unwrap_or(s.len());

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Rust control and for in</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Tue, 21 May 2024 05:35:00 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust-control-3943</link>
      <guid>https://dev.to/zhangwuji/rust-control-3943</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let  mut hh=Person {
        name:String::from("jack")
};

 let  a = vec![ hh];
 a[0].name=String::from("jack");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the element inner vec's control is managed by vec instance；&lt;br&gt;
So even though hh variable is mutable,but it is not useful.&lt;br&gt;
a variable is mutable or immutable that really work;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 let mut a = vec![ hh];
 let refrenceA=&amp;amp;a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;refrenceA variable's type is a immutable reference；&lt;br&gt;
No matter a variable is mutable or immutable, Only it' refrence like&lt;code&gt;&amp;amp;a&lt;/code&gt; is default setted up to mutable refrence. if want to become mutable refrence； &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&amp;amp; mut a； and mut a&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>rust vector and for</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Tue, 21 May 2024 04:59:53 +0000</pubDate>
      <link>https://dev.to/zhangwuji/study-rust-1a8p</link>
      <guid>https://dev.to/zhangwuji/study-rust-1a8p</guid>
      <description>&lt;p&gt;1.&lt;br&gt;
Vector is an array it's length can be expended。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//empty Vector
let mut a = Vec::new();
// init with one element at least
let mut a1= vec![1,2,33,44,55];
// the elements inner vec instace must be same type
enum One {
   Ha,
   String(String),
   Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from("你好"))];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so how to traverse a1 it's type is vec;&lt;br&gt;
recommond for in;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum One {
   Ha,
   String(String),
   Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from("你好"))];
for item in a1 {

}

print!("{:#?}",{a1});

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

&lt;/div&gt;



&lt;p&gt;in a1 the control is given to “for”，So after excuted for statement;&lt;br&gt;
a1 was destroied;&lt;/p&gt;

&lt;p&gt;you must use &amp;amp;a in order to borrow the reference；like bellow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum One {
   Ha,
   String(String),
   Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from("你好"))];
for item in &amp;amp;a1 {

}

print!("{:#?}",{a1});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;for statement
any Type that implement Iterator trait can be traverse by for;
if the value was traverse is not a borrow refrence; like this
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   let  mut hh=Person {
        name:String::from("jack")
    };

    let mut a = vec![ hh];

for item in a{

}

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

&lt;/div&gt;



&lt;p&gt;if you want change the element in for statement;&lt;br&gt;
something remember;&lt;/p&gt;

&lt;p&gt;Even though a variable is mutable or immutable;&lt;br&gt;
the item is default setted up to immutable;&lt;br&gt;
so if you want item to be settted to mutable;&lt;/p&gt;

&lt;p&gt;Just like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; for mut item in  a{
        item.name =String::from("lo");
    };
or

 for mut item in  &amp;amp;a{
        item.name =String::from("lo");
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;followed situation is not allowed;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  for &amp;amp;item in  a{
        item.name =String::from("lo");
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;why？ let me analyse  which steps happen？&lt;/p&gt;

&lt;p&gt;one: take the element of a out of a;&lt;br&gt;
move the control of element to item;&lt;br&gt;
So &amp;amp; and item are combind into &amp;amp;item which regard as reference of the element。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a is not a borrow reference; so item'type expect to be the type of element; so this won't work.
2.if a is borrow reference;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  for &amp;amp;item in  &amp;amp;a{
        item.name =String::from("lo");
    };

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

&lt;/div&gt;


&lt;p&gt;reference a dose't have the control of a.So &amp;amp;a is not allowed to move the control of emenet to item; So this won't work.&lt;br&gt;
one situation will work;&lt;br&gt;
if the type implement copy trait; such as u8, u64..,bool,“char”,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   let a = vec![1,2,4,3];
    for &amp;amp;item in  &amp;amp;a{

    };

   let a = vec!["s","sa","sad"];


    for &amp;amp;item in  &amp;amp;a{
        let g=item;
    };

   let a = vec![true];


    for &amp;amp;item in  &amp;amp;a{
        let g=item;
    };

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

&lt;/div&gt;



&lt;p&gt;remember one strange situation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut a =vec![String::from("hello"),String::from("world")];
for  item in &amp;amp;mut a {
     item*=String::from("12312");
}

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

&lt;/div&gt;



&lt;p&gt;item* did not mean control moving. item* mean read from head. item* =  String::from("12312") mean write from head.&lt;br&gt;
follow case mean control moving;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a=*item
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>rust2</title>
      <dc:creator>Zhangwuji</dc:creator>
      <pubDate>Sun, 19 May 2024 23:22:51 +0000</pubDate>
      <link>https://dev.to/zhangwuji/rust2-1ph7</link>
      <guid>https://dev.to/zhangwuji/rust2-1ph7</guid>
      <description>&lt;p&gt;1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct animal {
   name:String
   age:u8
}

impl animal {
    // static function
    fn create(name:String,age:u8) -&amp;gt; animal {
         return {
            name:'jack',
            age:20
          }
    }
    // instance function
    fn say(&amp;amp;self){

    }


}

let animalInstance:animal = {
   name:"大灰狼",
   age：20
};
animalInstance.say();

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

&lt;/div&gt;



&lt;p&gt;enumerate Option&lt;br&gt;
it'appearance solve the problem of null point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let s = some(2);
let s2 = some(20);

if let some(x) = s {
    x == 2 //true
}else{
  None
};

match s {
   some(x) =&amp;gt; {
      x == 2
   },
   (_) =&amp;gt; {
     println!("我是none")
   }
};

match (s,s2) {
   (Some(s),Some(s2)) =&amp;gt; {
     s == 2 // true
     s2 == 20 // true
   },
   (_,(Some(S2))) =&amp;gt; {

  }
}

let x =0;
let y=1;

match(x,y){
   (0,1) =&amp;gt;{

   },
   (_,1)=&amp;gt;{

   }

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

&lt;/div&gt;



&lt;p&gt;package mechanism&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // lib.ts 
  // mod match2 {
      pub fn add(x:i8,y:i8) -&amp;gt; i32 {
          x+y
      }
   }

 // main.ts
 use  guessing_game::math2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
