DEV Community

Zhangwuji
Zhangwuji

Posted on

Rust

1.baseType in Rust follow
the basical datatypes are as follow

i8 i16 i32 i64 isize //signed integer
u8 u16 u32 u64 usize // unsigned integer
f32 f64 //float datatype
char // Occupies four bytes of unicode
bool

Enter fullscreen mode Exit fullscreen mode
let array:[u16;3] =[1,2,3]; // array database;
/*
* the type of element inner array must be same; 
*/
let one= array[0]

//Tuple

let ff:(string,u8,f32) = ("12",22,2.1221);
let one =ff.0;
let (one,two,three) = ff;
/*
* the type of element inner array does not have to be same

*/



Enter fullscreen mode Exit fullscreen mode
let s ="hello";
s's type is &type; &type is splice of type;
if it is string literal like bellow; it store in stack

let s2= String.from("12321");
let ff=&s2[0,2];
ff is a reference to s2.it store in head;
  let char_count = s.chars().count();
&s2[..] == &s2[0..char_count];
&s2[2..5] 
&s2[..5]=&s2[0..5]
Enter fullscreen mode Exit fullscreen mode
let fn2:fn(i32) -> i32 = fn (s:i32) {
   x + 1

}
Enter fullscreen mode Exit fullscreen mode

remember x + 1 without ; is expression;
But

x + 1;
Enter fullscreen mode Exit fullscreen mode

is statement end mark
In Rust only expression call default return;

expression as follow

if(x > 5){
   10
}else {
   20
}

{
  let a=10;
  let b=10;
  a+b
}

fu()


Enter fullscreen mode Exit fullscreen mode

statement as follow

let number = if x > 5 { 10 } else { 0 }; // if 表达式的值被绑定到 number

let z = {
    let a = 2;
    let b = 3;
    a + b // 代码块的值是 5
};

Enter fullscreen mode Exit fullscreen mode

object control

Image description
s3 s4 s5 dose not borrow s1 reference;
once they be passed to a function as a parameter. they will truely
borrow s1 reference.
such as

Image description;
so it mean
Image description
now gg is a mutable reference of s1;

  let gg= get_something(&mut s1);
  // 这里不会出现 borrow checker 错误了
    let s3=&mut s1;
Enter fullscreen mode Exit fullscreen mode


;
why dose not s3 throw error?
because gg and s3 dose not has truely make a reference; once

they be passed to a function as a parameter. they will truely
borrow s1 reference.they will true make refenrence;

1.remember the rules;
can not make reference of the same Object on the same time。
can not make mutable reference and immutable reference of the same Object on the same time。

Image description
it is ok;

Image description

Image description
the type of gg2 is &mut or & depending on the parameter of get_something instead of return_type of get_something;
because you pass a & to a function; the function can not return & inner it;like this
Image description because once the function called s2 be destroied as the time.
so if the function return any & in it; those & will ponit a object is destroied。

IN summary the type of gg2 is depending on parameter of the function;

on case is if the function pass many & as parameter;how dose the rust compiler know which & will be retun

Image description;
Image description

the function can write as follow

Top comments (0)