Rust doc book note

  1. ownership
    1. Rust calls drop automatically at the closing curly bracket.
    2. stack-only data Copy trait, older variable is still usable after assignment.
    3. ownership and type systems
  2. borrowing
    1. & is referencing, * is dereferencing
    2. &str ampersands are references, allow you to refer some value without taking ownership of it.
    3. &mut str is mutable reference
    4. slice
    5. at any given time, you can have either one mutable reference or any number of immutable references.
    6. references must always be valid.
  3. move
    1. 给变量换了个名字 原变量名失效
    2. stack-only data, Copy trait, still valid
  4. lifetime
    1. borrow checker借来的变量必须在变量的生命周期里
    2. input lifetimes: parameters; output lifetimes: return values;
    3. rule 1 each parameter that is a reference gets its own lifetime parameter.fn foo<'a>(x: &'a i32);fn foo<'a, 'b>(x: &'a i32, y: &'b i32);
    4. rule 2 if there is exactly one input lifetime parameter, that lifetime is assigned to all output lifetime parameter.fn foo<'a>(x: &'a i32) -> &'a i32;
    5. rule 3 if there are multiple input lifetime parameters, but one of them is &self or &mut self because this is a method, the lifetime of self is assigned to all output lifetime parameters.
    6. ' static reference can live for the entire duration of the program.
  5. method
    1. &self immutable reference
    2. &mut self mutable reference
    3. self consumer
    4. associated functions
  6. closure
    1. anonymous functions that can capture their environment.
    2. lazy evaluation
  7. trait
    1. defining a Trait for common behavior
    2. Copy. 与Drop互斥
    3. Clone.clone
    4. Deref. deref to behave like a reference. customize the behavior of the dereference operator, *
    5. DerefMut
    6. Drop. drop code that is run when a value goes out of scope. 需要做的额外的工作 释放堆上的数据. destructor method
  8. trait object
  9. struct
    1. similar to tuples, the pieces of a struct can be different types. Unlike with tuples, you’ll name each piece of data
    2. unit-like struct ()
    3. tuple struct struct MyBox<T>(T)
  10. zero-cost abstractions
  11. by which we mean using the abstraction imposes no additional runtime overhead
  12. In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better. –defines zero-overhead in “Foundations of C++” (2012)
  13. pointer
  14. a variable that contains an address in memory. This address refers to, or “points at,” some other data.
  15. smart pointers
    1. data structures that not only act like a pointer but also have additional metadata and capabilities.
    2. String, Vec<T>, Box<T>, Rc<T>, Ref<T>, RefMut<T>, RefCell<T>
  16. reference
    1. pointers that only borrow data. in contrast, in many cases, smart pointers own the data the point to.
    2. references must always be valid.
    3. regular reference or a type that implements Deref
  17. associated types
  18. Deref coercion
    1. happens automatically when we pass a reference to a particular type’s value as an argument to a function or method
    2. reference & 取地址 取引用
    3. dereference * 取内容 解引用
    4. resolved at compile time, so there is no runtime penalty for taking advantage of deref coercion.
    5. From &T to &U when T: Deref<Target=U>
    6. From &mut T to &mut U when T: DerefMut<Target=U>
    7. From &mut T to &U when T: Deref<Target=U>.
    8. converting an immutable reference to a mutable reference would require that there is only one immutable reference to that data, and the borrowing rules don’t guarantee that. Rust can’t make the assumption that converting an immutable reference to a mutable reference is possible.
  19. reference counting. Rc<T>
    1. 第一个人打开电视,最后一个人关闭电视
    2. only for use in single_threaded scenarios.
    3. Rc::clone(&a) doesn’t make a deep copy of all the data like most types’ implementations of clonedo.
    4. strong reference cycle can leak memory.
  20. interior mutating. RefCell<T>
  21. Weak<T>
    1. strong_count and weak_count; e.g. a parent node should own its children: if a parent node is dropped, its child nodes should be dropped as well. a child should not own its parent: if we drop a child node, the parent should still exist.
  22. concurrent and/or parallel
  23. thread
    1. race conditions,
    2. deadlocks,
    3. bugs that happen only in certain situations and are hard to reproduce and fix reliably
    4. 1:1 meaning one operating system thread per one language thread. Rust standard library
    5. M:N model, the green-threaded model. there are crates that implement M:N threading if you would rather trade overhead for aspects such as more control over which threads run when and lower costs of context switching,for example.
  24. move keyword
    1. to force the closure to take ownership of the values it uses in the environment.
  25. message passing
    1. “Do not communicate by sharing memory; instead, share memory by communicating.” – Go LANG
    2. channel, has two halves: a transmitter and a receiver.
  26. Mutex<T>: mutual exclusion
    1. you must attempt to acquire the lock before using the data.
    2. when you’re done with the data that the mutex guards, you must unlock the data so other threads can acquire the lock.
    3. the call to lock returns a smart pointer called MutexGuard
    4. implements Deref Drop
    5. provides interior mutability
    6. with risk of creating deadlocks (Rc<T> causing memory leak when reference cycle)
  27. Arc<T> atomically reference counted
    1. thread safety comes with a performance penalty
  28. Sync and Send Traits
    1. allowing transference of ownership between threads with Send
    2. allowing access from multiple threads with Sync
    3. any type composed entirely of Send types is automatically marked as Send as well
    4. types composed entirely of types that are Sync are also Sync
  29. object-oriented, namely objects, encapsulation, inheritance,
    1. Object-oriented programs are made up of objects, An object packages data and the procedures that operate on that data. The procedures are typically called method or operations. – The Gang of Four book
    2. encapsulation: modules, types, functions, and methods are private default
    3. inheritance: 1. reuse of code 2. to enable a child type to be used in the same places as the parent type. this is also called polymorphism, which means that you can substitute multiple objects for each other at runtime if they share certain characteristics.
    4. bounded parametric polymorphism
  30. trait object
    1. a trait object points to both an instance of a type implementing out specified trait as well as a table used to look up trait methods on that type at runtime.
    2. trait objects are more like objects in other languages in the sense that they combine data and behavior.
    3. their specific purpose is to allow abstraction across common behavior.
    4. using generics and trait bounds will monomorphized at compile time to use the concrete types. static dispatch
    5. using trait objects, dynamic dispatch
    6. duck typing in dynamically typed languages: if it walks like a duck and quacks like a duck, then it must be a duck!
    7. a trait is object safe if all the methods defined in the trait have the following properties: 1. the return type isn’t Self. 2. there are no generic type parameters.
    8. the trait std::clone::Clone cannot be made into an object break 1. the return type isn’t Self
  31. pattern
    1. refutable and irrefutable
  32. macro
    1. metaprogramming
    2. in C++, macro 没有类型检查和函数调用开销(inline)