most of the time, Rust’s lifetime is implicit and can be inferred
There are two types of life cycle: input life cycle and output life cycle
‘static is a special life cycle annotation
Example of lifetime out of scope
1 2 3 4 5 6 7 8 9
fnmain() { letmut x; { lety = String::from("hello"); // x = y; // this is allowed x = &y; // not allowed. borrowed value (y) does not live long enough } println!("Str:{}", x); }
lifetime checker
Rust compiler’s borrow checker to determine whether a borrow is legal
1 2 3 4 5 6 7 8
// If it returns a reference value, no matter how simple your function is written, it will always report an error `missing lifetime specifier.` fnlongest(x:&str, y:&str) -> &str { /// this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y` if x.len() > y.len() { x }else{ y } }
let’s find out why it is such case
1 2 3 4 5 6 7 8 9 10 11 12 13
fnmain() { // variable to hold the result value letlong_str;
letx = "abc".to_string(); { lety = "bbccd".to_string(); long_str = longest(x.as_str(), y.as_str()); } // if x.len() > y.len() then it is OK,the long_str variable will hole x; if not, long_str supposed tohold y, however, y has a smaller scope than x, long_str will hold to a dropped value println!("Longest str: {}", long_str); }
Hence, we need lifetime annotation '
1 2 3 4 5 6 7
fnlongest<'a>(x:&'astr, y:&'astr) -> &'astr { if x.len() > y.len() { x }else{ y } }
deeper understanding
When returning a reference value from a function, the lifetime of the return type needs to match the lifetime of one of the parameters
lifetime of the field part must be longer than struct
Lifetime Elision
In order to make common patterns more ergonomic, Rust allows lifetimes to be elided in function signatures. Elision rules are as follows:
Each elided lifetime in input position becomes a distinct lifetime parameter.
If there is exactly one input lifetime position (elided or not), that lifetime is assigned to all elided output lifetimes.
If there are multiple input lifetime positions, but one of them is &self or &mut self, the lifetime of self is assigned to all elided output lifetimes.
Otherwise, it is an error to elide an output lifetime.
// the first 'a is decraration, the second is usage impl<'a> ImportantExcepiton <'a> { // in return value, 'a is omitted according to Lifetime Elision rule fncallname(&self ) -> &str{ self.part } }
‘static
‘static is a special lifetime that takes up the duration of the entire program, for example all string literals have a ‘static lifetime
When the owner goes out of scope, the value will be dropped.
variable scope
1 2 3 4 5 6
fnmain() { { // s is not valid here, it’s not yet declared lets = "hello"; // s is valid from this point forward // do stuff with s } // this scope is now over, and s is no longer valid }
Move
stack-only data: Copy trait
such as primitive type
1 2 3 4
fnmain() { letx = 5; lety = x; }
bind the value 5 to x; then make a copy of the value in x and bind it to y.
ptr, len, capacity is stored in stack, while string value is stored in heap When we assign s1 to s2, the String data is copied, meaning we copy the pointer, the length, and the capacity that are on the stack. We do not copy the data on the heap
If we do want to deeply copy the heap data of the String, not just the stack data, we can use a common method called clone.
ownership and function
Passing a value to a function will result in a move or copy of ownership
difference between “stack” and “heap” variables: stack variables will be copied, and heap variables will be moved. When a variable containing heap data leaves the scope, its value will be cleared by the drop function, unless the ownership of the data is moved to another variable
fnmain() { lets = String::from("hello"); // s comes into scope takes_ownership(s); // s's value moves into the function... // ... and so is no longer valid here
letx = 5; // x comes into scope
makes_copy(x); // x would move into the function, // but i32 is Copy, so it's okay to still // use x afterward
} // Here, x goes out of scope, then s. But because s's value was moved, nothing // special happens.
fntakes_ownership(some_string: String) { // some_string comes into scope println!("{}", some_string); } // Here, some_string goes out of scope and `drop` is called. The backing // memory is freed.
fnmakes_copy(some_integer: i32) { // some_integer comes into scope println!("{}", some_integer); } // Here, some_integer goes out of scope. Nothing special happens.
fnmain() { lets1 = gives_ownership(); // gives_ownership moves its return // value into s1
lets2 = String::from("hello"); // s2 comes into scope
lets3 = takes_and_gives_back(s2); // s2 is moved into // takes_and_gives_back, which also // moves its return value into s3 } // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing // happens. s1 goes out of scope and is dropped.
fngives_ownership() ->String { // gives_ownership will move its // return value into the function // that calls it
letsome_string = String::from("yours"); // some_string comes into scope
some_string // some_string is returned and // moves out to the calling // function }
// This function takes a String and returns one fntakes_and_gives_back(a_string: String) ->String { // a_string comes into // scope
a_string // a_string is returned and moves out to the calling function }
What if we want to let a function use a value but not take ownership? that’s reference
reference & borrow
& means reference (borrow but not own), default immutable
&mut a mutable reference, only one mutable reference allowed in same scope (avoid data racing)
Multiple mutable references can be created non-simultaneously by creating a new scope
Cannot have mutable and immutable references at the same time
fnmain() { letmut s = String::from("hello"); { lets1 = &mut s; } // r1 goes out of scope here, so we can make a new reference with no problems. lets2 = &mut s; }
fnmain() { letmut s = String::from("hello");
letr1 = &s; // no problem letr2 = &s; // no problem println!("{} and {}", r1, r2); // variables r1 and r2 will not be used after this point
letr3 = &mut s; // no problem println!("{}", r3);
println!{"{}",r1} // got problem with above mutable borrow }
reference as function arguments
1 2 3 4 5 6 7 8 9 10 11 12
fnmain() { lets1 = String::from("hello");
letlen = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len); }
fncalculate_length(s: &String) ->usize { // s is a reference to a String s.len() } // Here, s goes out of scope. But because it does not have ownership of what // it refers to, nothing happens.
we pass &s1 into calculate_length and, in its definition, we take &String rather than String. These ampersands represent references, and they allow you to refer to some value without taking ownership of it. Because it does not own it, the value it points to will not be dropped when the reference stops being used. When functions have references as parameters instead of the actual values, we won’t need to return the values in order to give back ownership, because we never had ownership. We call the action of creating a reference borrowing.
A pointer refers to an address in memory, but the memory may have been freed and allocated for use by someone else
rust,The compiler can guarantee that there will never be dangling references
1 2 3 4 5 6 7 8
fnmain() { letr = dangle(); } fndangle() -> &string { // dangle returns a reference to a String lets = String::from("hello"); // s is a new String &s // we return a reference to the String, s } // Here, s goes out of scope, and is dropped. Its memory goes away. // Danger
The solution here is to return the String directly:
1 2 3 4 5 6 7 8
fnmain() { letstring = no_dangle(); }
fnno_dangle() ->String { lets = String::from("hello"); s }
This works without any problems. Ownership is moved out, and nothing is deallocated.
slice
Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection. A slice is a kind of reference, so it does not have ownership.
string slice
1 2 3 4 5 6
fnmain() { letmut s = String::from("Hello world");
lethello = &s[0..5]; letworld = &s[6..11]; }
Rather than a reference to the entire String, hello is a reference to a portion of the String, With Rust’s .. range syntax, if you want to start at index zero, you can drop the value before the two periods By the same token, if your slice includes the last byte of the String, you can drop the trailing number.
Note: String slice range indices must occur at valid UTF-8 character boundaries. If you attempt to create a string slice in the middle of a multibyte character, your program will exit with an error.
1 2 3 4 5 6 7 8 9
fnfirst_word(s :&String) -> &str { letbytes = s.as_bytes(); for(i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[..i]; } } &s[..] }
String Literals Are Slices
1 2 3
fnmain() { lets = "Hello, world!"; }
The type of s here is &str: it’s a slice pointing to that specific point of the binary.
String Slices as Parameters
Pass &str as a parameter, you can receive parameters of type &String and &str at the same time
isize, usize indicates that the type is determined by the architecture of the computer. For example, on a 32 bit target, this is 4 bytes and on a 64 bit target, this is 8 bytes.
0x: hex,0o Octal,0b binary,starting with b: byte (u8 only)
Number Literals
Example
Decimal
98_222
Hex
0xff
Octal
0o77
Binary
0b1111_0000
Byte(u8 only)
b’A’
Tuple
The length of Tuple is fixed, and the length cannot be changed once declared
1 2 3 4 5 6 7 8 9 10
fnmain() { // tuple could be declared as mut letmut tuple_1 = ("Hello", 39, "Years"); lettuple_2:(i32, &str ) = (1983, "since."); tuple_1.0 = "Hi"; println!("{} {} {}", tuple_1.0, tuple_1.1, tuple_1.2); // destructure let (a,b) = tuple_2; println!("{} {}", a, b); }
array
arrays in Rust have a fixed length.
Vector is similar to an array, it is provided by the standard library, and its length can be changed
1 2 3 4 5 6 7 8 9 10 11
fnmain() {
letarr_test:[u8; 3] = [1,2,3]; println!("Number is {},{},{}", arr_test[0],arr_test[1],arr_test[2]);
letarr_test = ["I","love","you"]; println!("You said : {} {} {}", arr_test[0],arr_test[1],arr_test[2]);
letarr_test = [1;3]; println!("Call Num : {}&{}&{}", arr_test[0],arr_test[1],arr_test[2]); }
String
Basic data types are stored on the stack, but the String type is stored on the heap
1
lets = String::from("hello");
push_str(): append a str slice a string
push(): appends a single character to a String
1 2 3 4 5
fnmain() { letmut data = String::from("andy"); data.push_str(" is stronger"); data.push('!'); }
+ operator, chaining strings. the left side of the + operator is the ownership of the string, and the right side is the string slice
String is actually a wrapper for Vec, so the length can be measured by the len() method, but note that Len() is not length of character, but byte len
String iteration
1 2 3 4 5 6 7 8 9 10 11 12 13
fnmain() { letmut data = String::from("andy"); data.push_str(" is stronger"); data.push('!');
foriin data.bytes() { /// }
foriin data.chars() { /// } }
Vector
Vector is like any other struct. When Vector leaves the scope, the variable value is cleaned up, and all its elements are also cleaned up.
1 2 3 4 5 6 7
fnmain() { letvec: Vec<u16> = Vec::new(); letvec2: Vec<i32> = vec![3,4,5] // create vector by macro foriin vec2 { println!("Vector value is : {}", i); } }
HashMap
HashMap is not preloaded, so it needs to be included use std::collections::HashMap
fnmain() { letcondition = 1; letx = if condition == 1 { "A" } else { "B" }; println!("Result x = {}" , x) ; }
loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14
fnmain() { letmut condition = 0;
letresult = 'outer: loop { // 'outer is label 'inner: loop { condition += 1; if3 == condition { break'outer3 * condition; // break outer loop } } }; println!("Loop result is : {}", result); /// Loop result is : 9 }
rot
1 2 3 4 5 6
fnmain() { letarr = [3,2,3]; fornumin arr.iter() { println!("For value is {}", num); } }
Range iterator
Range
1 2 3 4 5 6 7 8 9 10
fnmain() { fornumberin (1..=3) { println!("Number A is {}", number ); /// 1,2,3 } fornumberin (1..=3).rev() { /// rev means reverse, println!("Number B is {}", number ); /// 3,2,1 } }
struct
If struct is declared mutable then all fields in the instance are mutable
You can define a function that does not take self as the first parameter in the impl block. This form is called an associated function, and the calling method is similar to String::from()
If do not want to deal with Err, can use unwarp() method. If result is Ok(val), return val. If Err, then call the panic! macro.
expect can specify what the error message is, which is easier to debug
The question mark operator, ?
When writing code that calls many functions that return the Result type, the error handling can be tedious. The question mark operator, ?, hides some of the boilerplate of propagating errors up the call stack.