ZhangHanDong / tao-of-rust-codes

《Rust编程之道》随书源码
https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/
MIT License
1.18k stars 170 forks source link

[第七章] 7.1节: 面向对象风格编程 ref 关键字 #305

Open Kreedzt opened 3 years ago

Kreedzt commented 3 years ago

疑问

代码中的模式匹配使用了上下文都未提及的 ref 关键字. 此处是否应该有相关链接引用说明.

if let Some(ref fgcolor) = self.fgcolor {
    if has_wrote {
        res.push(';');
    }

    res.push_str(fgcolor.to_fg_str());
}

eg:

https://users.rust-lang.org/t/ref-keyword-versus/18818 https://doc.rust-lang.org/stable/rust-by-example/scope/borrow/ref.html https://stackoverflow.com/questions/58292554/what-can-ref-do-that-references-couldnt

ZhangHanDong commented 3 years ago

@Kreedzt 感谢反馈。 ref 在第二章应该提过吧?

Kreedzt commented 3 years ago

第二章中2.3.1节中: 只有一句话提及: 表达式的求值过程在不同的上下文中会有不同的结果. 求值上下文也分为 位置上下文 (Place Context) 和 值上下文(Value Context). 下面几种表达式属于位置上下文: ....

match 判别式或 let 绑定右侧在使用 ref 模式匹配的时候也是位置上下文.

ZhangHanDong commented 3 years ago

噢,可能忘记了,这个应该在模式匹配里讲一下的。只能第二版里增加了。

这个 ref 是模式匹配,匹配出 fgclolor的引用。