Rust in Action Notes Chapter 8 Network

  1. The diagram of P253 shows the protocols used at each layer of the networkinsert image description here
  2. Box<dyn std::error::Error>Indicates that a pointer points to a type that implements the standard error library, dynindicating that this is a trait object (trait object), which is an implementation of polymorphism in rust;
  3. Both trait objects and template objects (generics) are the way Rust implements polymorphism. Templates belong to static dispatch, which requires more disk space (disk space), but is faster (faster funtime); trait objects (trait objects) belong to dynamic dispatch (dynamic dispatch), which requires less space at runtime but introduces a little runtime overhead (addressing overhead of indirect pointers);
  4. There are three forms of trait objects in rust, &dyn Trait, &mut dyn Trait, Box<dyn Trait>the main difference is Box<dyn Trait>that they are owned trait objects, and the other two are borrowed. In the old version, there are also &Trait, Box<Trait>trait objects, which are now deprecated. The reason for encapsulation errors in function return values Box<dyn Trait>​​is that the size of the Box is known and can be stored on the stack;
  5. After Section 8.3, most of the feature objects are introduced through a simple RPG game;
  6. Section 8.4 describes how to use the standard library std::net::TcpStreamto create a tcp connection, and use the trust_dns library to resolve the domain name; usually bind the local address to 0.0.0.0:0, which means that the OS picks a random port to create a connection on this machine;
  7. The difference between using Vec::with_capacity(512)and vec![0;512]to create Vec<T> is that the length of the former is 0 and the capacity is 512, while the length and capacity of the latter are both 512;
  8. A question mark ?at the end of a function that returns Result<T,E> is a syntactic sugar. As an try!abbreviation of a macro, when the return result is Ok (T), it returns T, and when it returns Err (err), it try! / ?returns early and tries to convert err into the Error type of the return value of the calling function;
  9. For a new type implementation, std::error::Errorthe implementation part can be left blank, because std::error::Errorthere is a default implementation in it, map_err(f)which will map an error type to a function (function), where an implemented std::error::Errorenumeration error type can be regarded as a function;
  10. If you don’t want to use it map_err(), you can implement it for your own Error type std::convert::Fromto reduce overly verbose code; there is another way to deal with the return value of multiple Error types is to use unwrap()or expect(), but this method is not recommended for library authors, because users who use the library may encounter unexpected crashes;
  11. The MAC address has 6 bytes, which are divided into universally administered address and locally administered address. The global address is in the first 3 bytes of the MAC address, indicating the organization number, while the 6 bytes of the local address are the device number;
  12. The MAC address has two modes: unicast and multicast. The difference is that unicast will only allow the target MAC address to receive data frames. The setting method is to set the lowest bit of the first byte of the MAC address. Set it to 1 to indicate the broadcast mode. The following figure clearly illustrates the difference between the two modes:Figure 8.4
  13. Section 8.9 introduces the use of ip tuntap tool to create a virtual network device, which must be run in a Linux environment, see Page 282 for details; Section 8.10 implements a relatively low-level network processing tool from command line parameter processing, MAC address generation, DNS analysis, and HTTP request initiation with TCP primitives;

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131421974