![](/img/trans.png)
[英]Rust: Trait inference not working when variable is a reference
我正在 Rust 中實現一個鏈表,到目前為止,我發現讓節點指向其他節點或不指向任何內容的最佳方法是實現相同特征的多個結構。 (選項不起作用,因為我無法弄清楚如何在不使用選項的情況下修改選項內的項目。)為了確保所有節點都被擁有,我讓每個節點都擁有下一個節點但引用了前一個節點。 **是的,我知道我通 ...
[英]Rust: Trait inference not working when variable is a reference
我正在 Rust 中實現一個鏈表,到目前為止,我發現讓節點指向其他節點或不指向任何內容的最佳方法是實現相同特征的多個結構。 (選項不起作用,因為我無法弄清楚如何在不使用選項的情況下修改選項內的項目。)為了確保所有節點都被擁有,我讓每個節點都擁有下一個節點但引用了前一個節點。 **是的,我知道我通 ...
[英]What exactly is the requirement for "covering" a type & why does a single element tuple satisfy it?
假設存在以下代碼use core::any::Any; enum Value { Any(Box<dyn Any>), Other, // placeholder, this code is adapted from mine } 這段代碼引發了一個我不太明白的診斷 ...
[英]How to make type-erased version of a trait with associated type?
假設有一個集合特征,其項具有關聯類型:trait CollectionItem { // ... } trait Collection { type Item: CollectionItem; fn get(&self, index: usize) -& ...
[英]Can I have a collection over trait objects where their associated types implement a shared trait?
我知道通常你不能有像Vec<dyn Iterator>>這樣的東西,因為你當然需要指定關聯的類型。 現在,我當然可以擁有Vec<Box<dyn Iterator<Item=i32>>> 。 但是這樣的事情呢:trait Foo {} #[de ...
[英]Traits, Borrows and References in Rust
我是 Rust 的新手,仍在為借用、引用等問題苦苦掙扎。在測試特征時,我遇到了這個問題,但我不知道如何解決。 在調用將該實例作為參數傳遞的 function 之后,如何繼續使用該結構的實例? 我試過既將參數作為參考傳遞又不使用 & 但它們都不起作用。 ...
[英]How do I approach this trait object problem?
我的代碼類似於下面的代碼( 游樂場):trait SomeTrait { fn some_fn(&self) -> i32; } #[derive(Debug, Clone, Eq, PartialEq)] enum Foo { Bar, Baz(Box&l ...
[英]Trait object cannot be shared between threads safely
當我在 RUST 中編寫 zookeeper 客戶端時。 像這樣的代碼: 這失敗並顯示以下消息: 作為 RUST 的初學者,我無法弄清楚這個錯誤意味着什么以及如何修復它。 ...
[英]Why can't we make a trait with non-method associated functions into a trait object?
根據解釋,Rust 拒絕將具有非方法關聯函數的特征變成特征 object。 不能調用不帶self參數的方法,因為沒有辦法為它們獲取指向方法表的指針。 trait Foo { fn foo() -> u8; } 這可以稱為<Foo as Foo>::foo() ,它無法選擇實現。 ...
[英]Inconsistency of lifetime bound requirement when storing closures
當我嘗試將閉包存儲到HashMap時,我遇到了編譯器報告的生命周期約束要求。 這似乎是一個不一致的要求。struct NoBox<C: Fn() -> ()>(HashMap<String, C>); impl<C> NoBox<C> where ...
[英]Rust: can't collect an `Iterator<Item = Box<Dog>>` into `Vec<Box<dyn Animal>>`
我覺得這段代碼應該可以工作,因為Box<Dog>在大多數情況下應該能夠隱式轉換為Box<dyn Animal> :struct Dog {} trait Animal {} impl Animal for Dog {} fn main() { let _: Vec ...
[英]Rust struct with reference to a type with a dynamic parameter
這是我想要做的:struct Foo<T: Trait> { inner: T, } struct Bar<'a> { foo: &'a Foo<dyn Trait>, // This is pseudo code, I don't think ...
[英]Why is rustc's suggestion here wrong?
我正在嘗試實現以下特征,其中<'baz>是以后的impl<'baz> Baz<'baz> for T where T: OtherTrait †所必需的。 trait Baz<'baz>: Clone { fn baz(&'baz mu ...
[英]How to use the typestate pattern in other struct
我想使用 typestate 模式來定義幾個允許對它們中的每一個進行一些獨占操作的狀態。 我使用特征而不是枚舉來允許進一步的自定義。 因此,我可以使用這種模式,直到我嘗試將它包含在一個結構( Session部分)中,該結構在添加、更改或刪除文件時會發生突變。 trait IssueState {} ...
[英]Return Result<Box<dyn Trait>> in a match
我有一組實現給定特征的類型,我想從字符串名稱中獲取一個具體的類型對象,奇怪的是當我的match返回Box<dyn Trait>時它可以工作,但當我將它包裝在Result時它不會。 鑒於此特征和類型: trait Shape { fn edges(&self) -> ...
[英]Trait can't be made Object Safe when Hash + PartialEq are Supertraits
我是 Rust 的新手,所以這可能是一個簡單的問題。 我正在嘗試為 Handlers 創建一個小型注冊表,它應該返回任何實現TransferObject Trait 的結構:pub trait TransferObject: Hash + PartialEq {} 由於我存儲了在HashMap中注 ...
[英]Shared &str along multiple structs conflicts with Lifetimes
我有以下代碼: 這個想法是,當我調用 regex_clone 時,我得到一個與成員具有相同 &str 的新 Box<dyn Regex>,我認為因為我在調用 regex_clone 時只使用不可變引用,所以它會給我一個具有相同字符串切片的新結構,因為是一個參考我沒有移動任何東西, ...
[英]How do you implement a trait for T where T: Foo OR Bar
在 rust 中,您可以為任何實現了其他一些特征組合的類型自動實現特征。 IE:impl<T: Foo + Bar> SomeTrait for T { some_function(&self) { /*...*/ } } 我要做的是定義一種 ...
[英]Create vector of trait objects
我正在嘗試創建特征對象的向量,但出現類型不匹配。 我也嘗試使用.map()而不是 for 循環,但遇到了同樣的問題。 這是我真實代碼的最小版本,我意識到這個示例不需要使用特征對象,但我的真實代碼需要使用,因為 List 中除了Link之外還有其他類型,它們也實現了Speak 。 如何創建具有正確類型 ...
[英]Trait objects from references
下面是(大致)特征 object 示例,取自 rust 書第 17.2 章。 對於我的用例,我想在創建screen后繼續使用button和select_box (請參閱聲明screen后注釋掉的println!() ),但是我不能,因為button和select_box已移入screen 。 對我來 ...
[英]expected trait object `dyn Responsability`, found type parameter `T`
我正在嘗試在 Rust 中實現責任鏈: 鏈接到游樂場use std::error::Error; struct Query { query: String, } struct Response { response: u64, } trait Responsability ...