相关疑难解决方法(0)

如何在Rust中打印变量的类型?

我有以下内容:

let mut my_number = 32.90;
Run Code Online (Sandbox Code Playgroud)

我该如何打印my_number

使用typetype_of没有奏效.还有其他方法可以打印数字的类型吗?

types rust

204
推荐指数
11
解决办法
6万
查看次数

什么是Rust的确切自动解除引用规则?

我正在学习/试验Rust,在我用这种语言找到的所有优雅中,有一个让我感到困惑并且看起来完全不合适的特点.

在进行方法调用时,Rust会自动取消引用指针.我做了一些测试来确定确切的行为:

struct X { val: i32 }
impl std::ops::Deref for X {
    type Target = i32;
    fn deref(&self) -> &i32 { &self.val }
}

trait M { fn m(self); }
impl M for i32   { fn m(self) { println!("i32::m()");  } }
impl M for X     { fn m(self) { println!("X::m()");    } }
impl M for &X    { fn m(self) { println!("&X::m()");   } }
impl M for &&X   { fn m(self) { println!("&&X::m()");  } }
impl M for &&&X  { …
Run Code Online (Sandbox Code Playgroud)

reference dereference formal-semantics rust

152
推荐指数
2
解决办法
2万
查看次数

&T/&mut T 类型本身的复制/移动语义文档

我正在寻找有关引用和可变引用类型的复制/移动语义的文档。

以下代码片段显示不可变引用 ( & T) 实现了Copytrait 而可变引用 ( &mut T) 没有。

struct T;
fn copyable<U>(_: U) where U: Copy {}

fn main() {
    let a = &T;
    copyable(a);  // OK

    let b = &mut T;
    copyable(b);
    // error: the trait `core::marker::Copy` is not implemented for the type `&mut T`
}
Run Code Online (Sandbox Code Playgroud)

但我找不到这种行为的描述。有人知道一些(非)官方文件吗?(还是我错了?)

rust

8
推荐指数
2
解决办法
727
查看次数

标签 统计

rust ×3

dereference ×1

formal-semantics ×1

reference ×1

types ×1