rust

How to add constraints on generics

二次信任 提交于 2020-12-04 00:43:26
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

﹥>﹥吖頭↗ 提交于 2020-12-04 00:41:58
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

限于喜欢 提交于 2020-12-04 00:38:20
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

[亡魂溺海] 提交于 2020-12-04 00:38:17
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

VS code部署RUST运行环境

时光毁灭记忆、已成空白 提交于 2020-12-03 12:26:03
VS code部署RUST 开始之前 安装语言环境 搭建测试环境 结尾 开始之前 1、安装rust rust 安装地址 2、切换到 msvc toolchain by rustup default stable-x86_64-pc-windows-msvc 3、安装VSCODE VS Code 安装地址 安装语言环境 安装rls扩展 安装Native Debug扩展 扩展rust-analyzer 搭建测试环境 在工程目录下新建.vscode目录 在新建的 .vscode 文件夹里新建两个文件 tasks.json 和 launch.json,文件内容如下: tasks.json 文件 {     "version": "2.0.0",     "tasks": [         {             "label": "build",             "type": "shell",             "command":"cargo",             "args": ["build"]         }     ] } launch.json 文件(适用在 Windows 系统上) {     "version": "0.2.0",     "configurations": [         {             "name": "

What are the differences between the multiple ways to create zero-sized structs?

寵の児 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

杀马特。学长 韩版系。学妹 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

自作多情 提交于 2020-12-02 05:54:04
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

Using the Rust compiler to prevent forgetting to call a method

。_饼干妹妹 提交于 2020-11-30 12:17:10
问题 I have some code like this: foo.move_right_by(10); //do some stuff foo.move_left_by(10); It's really important that I perform both of those operations eventually , but I often forget to do the second one after the first. It causes a lot of bugs and I'm wondering if there is an idiomatic Rust way to avoid this problem. Is there a way to get the rust compiler to let me know when I forget? My idea was to maybe somehow have something like this: // must_use will prevent us from forgetting this if

Step by step interactive debugger for Rust?

点点圈 提交于 2020-11-30 02:17:08
问题 How can I debug Rust application step by step interactively like I'm able to do with "pry" in Ruby? I want to be able to see and preferably change the variables in real time when I reach a break point. Is there any production ready finished project? 回答1: I find a good level of usability with VS Code and the CodeLLDB extension: Install VS Code Search and install the extension Rust or the newer rust-analyzer from within VS Code Check requisites and setup CodeLLDB for your platform Search and