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
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
*/
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]
let fn2:fn(i32) -> i32 = fn (s:i32) {
x + 1
}
remember x + 1 without ; is expression;
But
x + 1;
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()
statement as follow
let number = if x > 5 { 10 } else { 0 }; // if 表达式的值被绑定到 number
let z = {
let a = 2;
let b = 3;
a + b // 代码块的值是 5
};
object control
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
;
so it mean
now gg is a mutable reference of s1;
let gg= get_something(&mut s1);
// 这里不会出现 borrow checker 错误了
let s3=&mut s1;
;
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。
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
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
the function can write as follow
Top comments (0)