我有几个线程正在等待Condvar
与包装在RwLock
. 我想Condvar::wait
在持有数据写入锁定后调用并检查条件,但它似乎Condvar::wait
只接受MutexGuard
作为参数。由于还有许多其他部分使用此数据作为只读变量,因此我不能简单地替换RwLock
为Mutex
.
我应该如何Condvar
与RwLock
-wrapped 数据一起使用?
我花了一段时间学习和搜索了Arrows,但对Arrow类的必要性感到有些困惑。据我所知,Arrow类是函数的抽象,而Arrow A a b c
表示某种东西需要b类型的输入和c类型的输出。此外,它提供了像一些基本的操作>>>
,arr
和first
。
但是,我找不到type的标准函数和type的b -> c
Arrow 之间的任何区别A a b c
。在我看来,first
并>>>
可以通过更换\(b, c) -> (f b, c)
,和(.)
。另外,由于箭头内的每个计算都由函数表示,因此如果我们用这些函数替换箭头,我认为不会有任何区别。
简而言之,我认为Arrows计算图的每个节点(类似于 https://www.haskell.org/arrows/syntax.html)都可以由Haskell的标准函数代替。如果属实,为什么我们使用Arrow代替函数?
我正在尝试使用货物构建rust-sfml,但是我的gcc无法找到所需的库.
我想通过将-L选项传递给gcc来解决这个问题,但我找不到任何方便的方法.
除了使用构建脚本之外,有没有可能的简单解决方案?
$ cargo run
Compiling sfml v0.9.3 (https://github.com/jeremyletang/rust-sfml#358f076a)
error: linking with `gcc` failed: exit code: 1
note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-static-libgcc" "-m64" "-L" "C:\Program Files\Rust stable 1.1\bin\rustlib\x86_64-pc-windows-gnu\lib" "-o" "D:\Programming\Rust\test_proj\target\debug\deps\sfml-4cd3c268d09fd120.dll" "D:\Programming\Rust\test_proj\target\debug\deps\sfml-4cd3c268d09fd120.o" "D:\Programming\Rust\test_proj\target\debug\deps\sfml-4cd3c268d09fd120.metadata.o" "D:\Programming\Rust\test_proj\target\debug\deps\libbitflags-ab4a647a363172d8.rlib" "D:\Programming\Rust\test_proj\target\debug\deps\liblibc-2eda841eb12a3090.rlib" "-L" "C:\Program Files\Rust stable 1.1\bin\rustlib\x86_64-pc-windows-gnu\lib" "-lstd-7d23ff90" "-L" "D:\Programming\Rust\test_proj\target\debug\deps" "-L" "D:\Programming\Rust\test_proj\target\debug\deps" "-L" "C:\Program Files\Rust stable 1.1\bin\rustlib\x86_64-pc-windows-gnu\lib" "-L" "D:\Programming\Rust\test_proj\.rust\bin\x86_64-pc-windows-gnu" "-L" "D:\Programming\Rust\test_proj\bin\x86_64-pc-windows-gnu" "-Wl,--whole-archive" "-Wl,-Bstatic" "-Wl,--no-whole-archive" "-Wl,-Bdynamic" "-lcsfml-system" "-lcsfml-window" "-lcsfml-audio" "-lcsfml-graphics" "-lcsfml-network" "-lws2_32" "-luserenv" "-shared" "-lcompiler-rt"
note: ld: cannot find -lcsfml-system
ld: cannot find -lcsfml-window
ld: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个包含两个哈希图的结构-一个具有一些值,一个引用另一个哈希图的元素。这是我的代码。
use std::collections::HashMap;
struct Foo;
struct Bar<'s>(&'s Foo);
struct MyStruct<'a> {
first : HashMap<&'a str, Foo>,
second : HashMap<&'a str, Bar<'a>>,
}
impl<'a> MyStruct<'a> {
fn new() -> MyStruct<'a> {
let mut result = MyStruct {
first : HashMap::new(),
second : HashMap::new(),
};
match result.first.get("") {
Some(t) => { result.second.insert("", Bar(t)); },
None => {},
}
result
}
}
fn main() {
}
Run Code Online (Sandbox Code Playgroud)
并且此代码无法编译,并出现以下错误:
错误:
result.first
寿命不足
我认为这个问题与一生有关,但是我无法清楚地解释出什么问题。
谁能解释正在发生的事情以及如何解决此错误?
PS结构Foo
和Bar
来自库,所以我不能修改这些结构。
我有一个名为的函数generateUID
,它是通过FFI连接的外部C函数.此函数为每个调用生成新的唯一ID,但我需要一个uid用于整个程序.如果我使用C/C++,我会创建一个像这样的函数.
int getUID() {
static int uid = generateUID();
return uid;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样使用它
int foo() { return getUID() + 1; }
int bar() { return getUID() + 2; }
int main() {
printf("%d\n", foo() + bar();
}
Run Code Online (Sandbox Code Playgroud)
在haskell中,我使用了这样的功能
getUID :: IO Int
getUID = generateUID -- this is attached to C lib with FFI
foo = (+1) <$> getUID
bar = (+2) <$> getUID
main = (+) <$> foo <*> bar >>= print
Run Code Online (Sandbox Code Playgroud)
但是,getUID
如果我使用此代码,则会调用两次.我所知道的唯一解决方案是将它们合并为一种do
符号,但是在我的实际代码中, …
根据文档,Vec<T>
实现Sync
if T
implements Sync
.它似乎是由一些魔法自动生成的,但我觉得这是违反直觉的,因为矢量的天真实现不是线程安全的.
真的Vec<T>
在Rust Sync
吗?
我正在尝试编写代码来获取某个向量的最后一个元素,并根据该元素执行不同的操作(包括向量的变异).
我试过这样的:
#[derive(Clone, PartialEq)]
enum ParseItem {
Start,
End,
}
let mut item_vec = vec![ParseItem::End];
loop {
let last_item = *item_vec.last().clone().unwrap();
match last_item {
ParseItem::End => item_vec.push(ParseItem::Start),
_ => break,
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误:无法移出借来的内容
让last_item =*item_vec.last().clone().unwrap();
我认为通过克隆item_vec.last()
,所有权问题将得到解决,但似乎没有.
如果我用这样的整数向量尝试相同的东西:
let mut int_vec = vec![0];
loop {
let last_int = *int_vec.last().clone().unwrap();
match last_int {
0 => int_vec.push(1),
_ => break,
}
}
Run Code Online (Sandbox Code Playgroud)
编译器不会抱怨借用.
为什么我的代码无法编译?