You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/rust-lang book/rust-lang-book-notes.md
+47-9
Original file line number
Diff line number
Diff line change
@@ -242,9 +242,7 @@ fn makes_copy(some_integer: i32) {// some_integer comes into scope
242
242
243
243
- returning values can also transfer ownership; assigning a value to another variable moves it
244
244
- when a variable that includes data on the heap goes out of scope, the value will be cleaned up by `drop` unless the data has been moved to be owned by another variable
245
-
-
246
-
247
-
**4.2 References and Borrowing**
245
+
-**4.2 References and Borrowing**
248
246
249
247
-`&` = references, allow you to refer to some value without taking ownership of it
250
248
- rules of references - at any given time, you can have _either_ one mutable reference _or_ any number of immutable references - references must always be valid
@@ -975,11 +973,9 @@ fn expensive_test() {
975
973
- iterators produce a series of values,
976
974
-`collect`: turns iterator into a collection that contains all elements
977
975
- needs to have annotation because Rust isn't able to infer collection type
978
-
-
976
+
-**12.2 Reading a File**
979
977
980
-
**12.2 Reading a File**
981
-
982
-
**12.3 Refactoring to Improve Modularity and Error Handling**
978
+
**12.3 Refactoring to Improve Modularity and Error Handling**
983
979
984
980
- primitive obsession: anti pattern where using primitive values when a complex type would be more appropriate
985
981
-`unwrap_or_else`: allows to define some custom, non-`panic!` error handling.
@@ -1018,6 +1014,7 @@ fn expensive_test() {
1018
1014
- explicit type annotations on parameters and functions not required, because closures are usually short and relevant only within a narrow context rather tan in any arbitrary scenario
1019
1015
- unlike function, can capture their environment and can access variables from the scope in which they're defined
1020
1016
- when a closure captures a value from it's environment, it uses memory to store the values for use in the closure body
1017
+
-
1021
1018
- closures can capture values from their environment in three ways, which directly map to the three ways a function can take a parameter: taking ownership , borrowing mutably, and borrowing immutably
1022
1019
- associated traits:
1023
1020
-`FnOnce`: taking ownership
@@ -1036,10 +1033,51 @@ let expensive_closure = |num| {
1036
1033
```
1037
1034
1038
1035
- reason for using closure: defined the code to call at one point, store that code, and call it at a later point
1039
-
-
1036
+
-**13.2 Processing a Series of Items with iterators**
1037
+
1038
+
- iterator
1039
+
- responsible for the logic of iterating over each item and determining when the sequence has finished
1040
+
- lazy: they have no effect until you call methods that consume the iterator to use it up
1041
+
1042
+
```rust
1043
+
letv1=vec![10, 20, 30];
1044
+
forvalinv1_iter {
1045
+
println!("val is {} ", val);
1046
+
}
1047
+
// will print
1048
+
// val is 10
1049
+
// val is 20
1050
+
// val is 30
1051
+
```
1040
1052
1041
-
**13.2 Processing a Series of Items with iterators**
1053
+
- all iterators implement the `Iterator` trait (in standard library)
1054
+
- if using the `next` method from `Iterator`, the data structure you're calling it on must be mutable
1055
+
-`next` method changes the internal state that the iterator uses to keep track of where it is in the sequence
1056
+
- methods that call `next` are called _consuming adaptors_, because calling them uses up the iterator
1057
+
-_iterator adaptors_: allow you to change iterators into different kinds of iterators
1058
+
-`collect`: consumes the iterator and collects the resulting values into a collection data type
1059
+
1060
+
```rust
1061
+
letv1:Vec<i32> =vec![1, 2, 3];
1062
+
letv2:Vec<_> =v1.iter().map(|x|x+1).collect();
1063
+
```
1042
1064
1043
1065
**13.3 Improving Our I/O Project**
1044
1066
1045
1067
**13.4 Comparing Performance: Loops vs Iterators**
1068
+
1069
+
- iterators get compiled down to roughly the same code as if you'd written the lower-level code yourself
1070
+
- Iterators are one of Rust’s _zero-cost abstractions_, by which we mean using the abstraction imposes no additional runtime overhead.
1071
+
-_Unrolling_: optimization that removes the overhead of the loop controlling code and instead generates repetitive code for each iteration of the loop.
1072
+
1073
+
### Chapter 14 - More About Cargo and Crates.io
1074
+
1075
+
-**14.1 Customizing Builds with Release Profiles**
1076
+
1077
+
**14.2 Publishing a Crate to Crates.io**
1078
+
1079
+
**14.3 Cargo Workspaces**
1080
+
1081
+
**14.4 Installing Binaries from Crates.io with cargo install**
0 commit comments