Rust programming video tutorial (Advanced) – 011_ 3 dereference polymorphism

Time:2021-10-15

Video address

Headline address:https://www.ixigua.com/i677586170644791348…
Station B address:https://www.bilibili.com/video/av81202308/

Source address

GitHub address:https://github.com/anonymousGiga/learn_rus…

Explanation content

1. Dereference forced polymorphism
Example: follow the example above

fn hello(name: &str) { 
    println!("Hello, {}!", name); 
}

fn main() { 
    let m = MyBox::new(String::from("Rust")); 
    hello(&m); // When dereferencing here, force polymorphism and change & string to & str. otherwise, you need: Hello (& (* m) [..]; 
}

2. Dereference polymorphism and variability interaction
There are three cases of dereference polymorphism:

  • When   T: Deref   Shi Cong  & T   reach  & U。
  • When   T: DerefMut   Shi Cong  & mut T   reach  & mut U。
  • When   T: Deref   Shi Cong  & mut T   reach  & U。 (Note: the opposite is impossible here)

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed

Recommended Today

VBS tutorial: objects – regular expression (regexp) objects

Regular expression (regexp) objectProvides simple regular expression support. explainThe following code illustrates the usage of regexp object: Function RegExpTest(patrn, strng) Dim regex, match, matches’ create variables. Set regex = new regexp ‘establish a regular expression. Regex. Pattern = pattern ‘sets the pattern. Regex.ignorecase = true ‘sets whether characters are case sensitive. Regex. Global = true […]