advance/smart-pointer/deref #732
Replies: 30 comments 57 replies
-
干巴巴讲的挺好的,讲到标准库我就不理解了, impl<T: ?Sized> Deref for &T {
type Target = T;
fn deref(&self) -> &T {
*self
}
} |
Beta Was this translation helpful? Give feedback.
-
最新的rust “ assert_eq!(5, *y); ” 是否已经是相等的了? 执行了一遍发现没有告警了。从语义上讲的确 *(&y) 应该等于 y |
Beta Was this translation helpful? Give feedback.
-
let str1: &str = "hello"; println!("str1: {}, str2: {}", str1, str2); |
Beta Was this translation helpful? Give feedback.
-
有一处疑惑的地方,希望各位不吝赐教 |
Beta Was this translation helpful? Give feedback.
-
struct MyBox(T); } fn main() { let c=Box::new("123".to_string()); |
Beta Was this translation helpful? Give feedback.
-
这本书是英文版翻译过来的吧? |
Beta Was this translation helpful? Give feedback.
-
下面的我的理解,不知道对否? struct Foo;
impl Foo {
fn foo(&self) { println!("Foo"); }
}
let f = Foo;
// 这里发生了自动的*引用*。这个机制似乎跟文章里提的这些没有关系,是针对self的独特机制。
f.foo(); // 发生了f -> &f
// 这里发生了自动的*解引用*,也就是文章里的隐式Deref转换
(&&f).foo(); // 发生了&&f -> &f |
Beta Was this translation helpful? Give feedback.
-
是不是只有加上 let a = Box::new(3);
println!("a = {}", a); // 这句话发生了隐式解引用吗? |
Beta Was this translation helpful? Give feedback.
-
因为一般教程讲到Deref的时候都是从解引用操作开始的,比如本文中的(y.deref())那段,所以总让人迷惑。 |
Beta Was this translation helpful? Give feedback.
-
Deref for &T的实现模版 impl<T: ?Sized> const Deref for &T {
type Target = T;
#[rustc_diagnostic_item = "noop_method_deref"]
fn deref(&self) -> &T {
*self
}
} Clone for &T的实现模版 impl<T: ?Sized> const Clone for &T {
#[inline(always)]
#[rustc_diagnostic_item = "noop_method_clone"]
fn clone(&self) -> Self {
*self
}
} 为什么两个“相同”的函数的语义是不一样?clone(&self)->Self和dere(&self)->&T只有返回值的形式不一样,一个是Self,一个是&T。难道编译器对‘引用&’有特殊处理? |
Beta Was this translation helpful? Give feedback.
-
其实就是c++里的重载*运算符吧? |
Beta Was this translation helpful? Give feedback.
-
有朋友知道,Rust 的Deref里,这个 self.0 是什么用法么? |
Beta Was this translation helpful? Give feedback.
-
fn main() { fn display(s: &str) { |
Beta Was this translation helpful? Give feedback.
-
关于 #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
type Target = T;
fn deref(&self) -> &T {
&**self
}
} 我的理解是 |
Beta Was this translation helpful? Give feedback.
-
impl<T: ?Sized> Deref for &T {
} 这个还是不懂。如果有一个是&&&&T 如果一个self类型。 |
Beta Was this translation helpful? Give feedback.
-
我觉得应该强调一下因果关系: |
Beta Was this translation helpful? Give feedback.
-
可以看下这篇文章: impl<T> Box<T> {
pub fn new(x: T) -> Self {
#[rustc_box]
Box::new(x)
}
...
...
... |
Beta Was this translation helpful? Give feedback.
-
首先 MyBox 被 Deref 成 String 类型,结果并不能满足 display 函数参数的要求,编译器发现 String 还可以继续 Deref 成 &str |
Beta Was this translation helpful? Give feedback.
-
本来还好,看到这有些迷茫了 impl<T: ?Sized> Deref for &T {
type Target = T;
fn deref(&self) -> &T {
*self
}
} 理解了一下,相当于** 然后又联想到了 let s: String = String::new();
let _a: &str = String::deref(&s);
let _a: &str = (&s).deref();
let _a: &str = (s).deref(); 以上代码分别利用完全限定语法以及点语法,现在只考虑点语法, |
Beta Was this translation helpful? Give feedback.
-
如果 let v = Box::new(vec![10, 20, 30]);
{
let a = *v;
let b = *(v.deref());
} 变量 |
Beta Was this translation helpful? Give feedback.
-
下面一个demo,比较简短见证了 &mut Box 自动转成 &mut String fn main() {
let mut s = Box::new(String::from("hello, "));
display(&mut s)
}
fn display(s: &mut String) {
s.push_str("world");
println!("{}", s);
} |
Beta Was this translation helpful? Give feedback.
-
开头的问题怎么理解,name是如何匹配的
似乎是&&mut Person转化为&&Person,再解引用微&Person,最后匹配Person{name, age} = Person{&name, &age),因此name类型是&string或者&str |
Beta Was this translation helpful? Give feedback.
-
刚刚看到了一个类似的特征 AsRef (转引用)
https://dev.to/zhanghandong/rust-concept-clarification-deref-vs-asref-vs-borrow-vs-cow-13g6 |
Beta Was this translation helpful? Give feedback.
-
对于Box,为什么*(box.deref()) 无法被转移所有权(很合理),而 *box 可以被转移所有权(???),这是什么原理? |
Beta Was this translation helpful? Give feedback.
-
感觉(通过 * 获取引用背后的值)[https://course.rs/advance/smart-pointer/deref.html#%E9%80%9A%E8%BF%87--%E8%8E%B7%E5%8F%96%E5%BC%95%E7%94%A8%E8%83%8C%E5%90%8E%E7%9A%84%E5%80%BC]在讲引用的时候讲可以让有c/cpp经验的人更好的理解Rust的引用,更好的理清引用关系 |
Beta Was this translation helpful? Give feedback.
-
在这段源码中,&T 被自动解引用为 T,也就是 &T: Deref<Target=T> , 这里是不是&&T->&T呀 |
Beta Was this translation helpful? Give feedback.
-
看中间的例子时总是想 fn main() {
let s = Box::new(String::from("hello, world"));
let s1: &String = &s; // Ok, &Box<String> -> &String
let s2: &str = &s; // Ok, &Box<String> -> &String -> &str
// let s3: String = &s; // Err, &Box<String> -> &String -x-> String
let s4: String = *s; // Ok, Box<String> -> String. (`s` moved, can't be used later)
let s5: &str = &s4; // Ok, String -> &str
} |
Beta Was this translation helpful? Give feedback.
-
eq函数没有实现引用归一化吗? |
Beta Was this translation helpful? Give feedback.
-
大家如果使用VSCode的rust-analyzer插件,可以在settings.json里面加入这些: "rust-analyzer.inlayHints.reborrowHints.enable": "always",
"rust-analyzer.inlayHints.bindingModeHints.enable": true,
"rust-analyzer.inlayHints.closureCaptureHints.enable": true,
"rust-analyzer.inlayHints.closureReturnTypeHints.enable": "always",
"rust-analyzer.inlayHints.expressionAdjustmentHints.enable": "always",
"rust-analyzer.inlayHints.lifetimeElisionHints.enable": "always",
"rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames": true,
"rust-analyzer.inlayHints.discriminantHints.enable": "always", 这样就可以让rust-analyzer自动把这些隐式的语法糖转换给显示出来了。 |
Beta Was this translation helpful? Give feedback.
-
为什么这里调用 fn main() {
let a = [1, 2, 3, 4];
let b = &a;
hello_world(&b);
}
fn hello_world(a: &[i32]) {
} 报错显示 mismatched types
expected reference `&[i32]`
found reference `&&[{integer}; 4]` rust 不是能自动解多层引用吗? |
Beta Was this translation helpful? Give feedback.
-
advance/smart-pointer/deref
https://course.rs/advance/smart-pointer/deref.html
Beta Was this translation helpful? Give feedback.
All reactions