[英]Can I reuse a struct field during type conversion without calling .clone()?
我目前有兩個相似的結構,我需要實現From特性以將一個轉換為另一個。 但是,我收到一條錯誤消息:error[E0382]: borrow of moved value: `item.my_first_string` --> src/lib.rs:14:30 | 13 | ...
[英]Can I reuse a struct field during type conversion without calling .clone()?
我目前有兩個相似的結構,我需要實現From特性以將一個轉換為另一個。 但是,我收到一條錯誤消息:error[E0382]: borrow of moved value: `item.my_first_string` --> src/lib.rs:14:30 | 13 | ...
[英]Rust Compiler throws error related to moving data out of a shared reference
我在 Rust 中編寫了一個程序,遇到了與移動共享引用數據相關的錯誤。 我做了一些研究,但我無法在我編寫的程序中找到錯誤的原因。 這是該程序的簡化版本:enum A { Won = 1, } struct B { result: A, } fn print_game(game: ...
[英]Use of moved value `inb`
我正在嘗試寫一個 function,但我收到一條關於借用的錯誤消息,我無法解決,因為我正在努力思考這個網站上的其他回復。 這是我到目前為止的 function:use orgize::Org; fn convert(i: String) -> String { let inb: V ...
[英]In Rust, what is the idiomatic way to install a mutable callback function on multiple subobjects at the same time?
我有一個以某種方式操縱對象數組的算法,但調用者需要能夠偵聽由該算法觸發的某些事件(對象的更新)。 以下是我正在嘗試做的一個簡化示例。 ( Rust游樂場) 這是算法模塊:// Some module containing the algorithm. // This module doesn't ...
[英]Returning a pointer to a boxed value from a collection in Rust
我收藏了盒裝object: 我有一個 function 需要返回一個 *mut MyObject,它看起來很像這樣。 但是我在返回行的編譯過程中遇到錯誤: ...
[英]Borrow Checker not releasing borrow from FnOnce callback
為什么編譯:fn func<T>( callback: impl FnOnce(&mut i64) -> T, ) -> T { let v = 42; callback(&mut 42) } 但這不是嗎?:fn func<'a, ...
[英]`&` reference, so the data it refers to cannot be borrowed as mutable
這是我的代碼: 這是我從編譯器得到的錯誤信息: 我將let Some(parent) == &self.parent為let mut Some(parent) == &self.parent ,但編譯器會拋出語法錯誤。 我該如何解決這個錯誤? ...
[英]Rust Rc<RefCell>::borrow_mut returns &mut Rc<RefCell<T>> instead of RefMut<T>
所以我在 Rust 中相對較新,我試圖在 C++ 中獲得與 std::shared_ptr 類似的東西。我決定使用 Rc<RefCell> 模式的 go。 我正在嘗試獲取和修改Rc<RefCell<i32>>的值,但borrow_mut()一直返回&mu ...
[英]Drop a borrow reference while keeping the underlying allocated memory
想象一下在這種情況下,我基本上將數據存儲在堆上。let layout = Layout::new::<usize>(); let data: usize = 1; let data_ptr = alloc(layout) as *mut usize; ptr::write(data_p ...
[英]Unexpectedly borrowed value does not live long enough after method call
我正在嘗試迭代加深的有限 state 空間,同時保持一組可見狀態。 以下 function 不完整,但定義足以證明我遇到的問題。 這是 get_successors 的簽名。 返回的ValveState不引用self (或者至少不應該)。 fn get_successors(&self) - ...
[英]Borrow issue when returning error value in match statement
警告,非常新手 Rust 編碼器在這里。 我確信這只是我對所有權如何運作的一個基本誤解,但我似乎無法理解為什么這段代碼會遇到錯誤[E0502]:不能將self.bids借用為可變的,因為它也被借用為不可變的. 更新:已將代碼片段更改為可重現的內容。 我認為在“檢查”的匹配語句的 Ok() arm 中 ...
[英]Value moved into closure in previous iteration of loop
我正在嘗試在我正在實施的小型光線追蹤器中使用並行化。 這個想法是我使用 function cast_ray()逐像素生成圖像,所以我想為每個像素生成一個新線程以加快計算速度。 基本上,我正在計算給定大小的二維數組。 我使用 Messages 來更新帶有顏色值的數組。 我的渲染器 function 看 ...
[英]Rust: what is different in the slice clone method?
來自這個有效的代碼模板:{ fn f3( _s : &String) {} fn f( s : &String) -> impl FnMut() { let s2 = s.clone(); move || f3( &s2) } let ...
[英]How to map to references if it's not an Iterator<Item=&T>?
我有一個接收 Iterator<Item=AsRef> 的 function,我想從這個迭代器中獲取一個子字符串數組。 問題是在映射時, .map()獲得了 AsRef 的所有權,而我無法返回as_ref() 。 我怎樣才能把這個迭代器變成像 Vec::iter 這樣的引用迭代器? 示 ...
[英]Rust error[E0716]: temporary value dropped while borrowed
我之前去過這個問題,因為問題和答案非常有用。 然而,我的情況似乎有些不同。 因此,這篇文章: 因為這個例子(以及附帶的解決方案/解釋)與我的案例非常相似,所以我想在這里添加它(而不是問一個新問題)。 我只是想讀取目錄的條目(Ubuntu Linux 22.10,ext4 文件系統): 不是很慣用,我 ...
[英]Using reference lifetimes correctly in iterator
我正在嘗試從某個結構s1中創建一個迭代器,它在其中有一些生命周期的引用,比如'a和'b 。 迭代器元素是具有相同引用的相同結構類型的新實例。 當我嘗試這樣做時,迭代器元素似乎受制於s1的生命周期,而不僅僅是'a和'b 。 為清楚起見的示例:#[derive(Debug)] pub struct My ...
[英]Rust: Is it safe to cast a mutable borrow to a pointer and back (to placate the borrow checker), if I know that there will only be one instance of it?
我有一個使用四叉樹的程序。 這棵樹存儲對另一個容器(Vec)擁有的數據的可變借用。 我在每個游戲循環中重建四叉樹,但我不想重新分配,所以我clear了四叉樹的底層 Vec,而不是從頭開始重建它。 下面顯示了一個演示相同問題的簡化示例。 在這里我只是使用另一個 Vec 而不是 QuadTree,因為 ...
[英]Why can I move out of mutable self reference for Read?
我不明白為什么這段代碼可以編譯:use std::io::{Read, BufRead}; trait ReadString { fn read_null_terminated_string(&mut self, max_size: u64) -> std::io::Res ...
[英]cannot return value referencing local data
我是 rust 的新手。下面的 get_x509 function 創建了編譯器警告“無法返回引用本地數據pem.contents的值”。 我想我明白為什么 - 因為返回值引用 pem.contents ,它僅在 scope 中用於 function - 但我無法弄清楚如何讓它工作。 下面代碼中的 ...
[英]Iterative tree parsing in Rust
假設我們有一棵樹:#[derive(Default, Debug, PartialEq, Eq)] struct Tree { children: Vec<Tree>, } 我們想從bool的列表中構建它,其中true就像一個打開標記, false就像 XML 中的一個關閉標記 ...