我目前正在学习 Rust,作为第一个练习,我想实现一个计算第 n 个斐波那契数的函数:
fn main() {
for i in 0..48 {
println!("{}: {}", i, fibonacci(i));
}
}
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
Run Code Online (Sandbox Code Playgroud)
我将其运行为:
fn main() {
for i in 0..48 {
println!("{}: {}", i, fibonacci(i));
}
}
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + …Run Code Online (Sandbox Code Playgroud) 我最近一直在学习 Rust,并正在使用 glfw3 和 gl 开发 3d 引擎。我已经尝试获得一个可以在我的 C++ 引擎中运行的着色器程序,并且可以在我的 Rust 引擎上运行,但在设置着色器制服的实现上遇到了障碍。
着色器.h:
template <typename T> void SetUniform(const GLchar* name, const T& data);
Run Code Online (Sandbox Code Playgroud)
着色器.cpp
template <> void ShaderProgram::SetUniform<int>(const GLchar* name, const int& data) {
glUniform1i(GetUniformLocation(name), data);
}
template <> void ShaderProgram::SetUniform<float>(const GLchar* name, const float& data) {
glUniform1f(GetUniformLocation(name), data);
}
template <> void ShaderProgram::SetUniform(const GLchar* name, const glm::vec2& data) {
glUniform2f(GetUniformLocation(name), data.x, data.y);
}
//etc...
Run Code Online (Sandbox Code Playgroud)
由于我对 Rust 的了解有限,我尝试使用 match 语句,因为它到目前为止为我节省了很多麻烦,但没有成功。
pub fn set_uniform<T>(&mut self, value: T){
match T {
u32 …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据条件选择要调用的函数.我想将该函数存储在一个变量中,这样我以后可以再次调用它而不需要携带条件.这是一个有用的最小例子:
fn foo() {
println! ("Foo");
}
fn bar() {
println! ("Bar");
}
fn main() {
let selector = 0;
let foo: &Fn() = &foo;
let bar: &Fn() = &bar;
let test = match selector {
0 => foo,
_ => bar
};
test();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否有可能摆脱中间变量?我试过简单地删除它们:
fn foo() {
println! ("Foo");
}
fn bar() {
println! ("Bar");
}
fn main() {
let selector = 0;
let test = match selector {
0 => &foo as &Fn(),
_ => &bar as …Run Code Online (Sandbox Code Playgroud) 该Option::and_then函数允许简化此代码:
let foo = Some(1);
let bar = match foo {
Some(i) => Some(i + 1),
None => None,
};
println!("Foo: {:?}", foo);
Run Code Online (Sandbox Code Playgroud)
进入这个:
let foo = Some(1);
let bar = foo.and_then(|i| Some(i + 1));
println!("Foo: {:?}", foo);
Run Code Online (Sandbox Code Playgroud)
如果我用Strings 尝试相同的东西,它不会编译:
let foo = Some("bla".to_string());
let bar = foo.and_then(|ref f| Some(f.clone()));
println!("Foo: {:?}", foo);
Run Code Online (Sandbox Code Playgroud)
error[E0382]: use of moved value: `foo`
--> src/main.rs:4:27
|
3 | let bar = foo.and_then(|ref f| Some(f.clone()));
| --- value moved here
4 …Run Code Online (Sandbox Code Playgroud) 我试图使用ripgrep_all安装cargo install ripgrep_all。它给出了以下错误:
% cargo install ripgrep_all
Updating crates.io index
Installing ripgrep_all v0.9.6
error: failed to compile `ripgrep_all v0.9.6`, intermediate artifacts can be found at `/tmp/cargo-install5HlOMt`
Caused by:
failed to select a version for the requirement `cachedir = "^0.1.1"`
candidate versions found which didn't match: 0.3.0, 0.2.0
location searched: crates.io index
required by package `ripgrep_all v0.9.6`
Run Code Online (Sandbox Code Playgroud)
然后我搜索了一下,发现:
看起来cachedir 已经撤回了0.1.1 版本。
解决方案是:
cargo install --locked ripgrep_all
Run Code Online (Sandbox Code Playgroud)
我能够成功安装它。然而,在安装过程中它说:
% cargo install --force --locked ripgrep_all
Updating crates.io …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照此过程https://docs.substrate.io/tutorials/build-a-blockchain/build-local-blockchain/#start-the-local-node启动本地节点。但它总是给我错误:
\n\xe2\x9e\x9c substrate-node-template git:(my-wip-branch) ./target/release/node-template --dev\n\n2023-04-07 12:59:26 Substrate Node \n2023-04-07 12:59:26 \xe2\x9c\x8c\xef\xb8\x8f version 4.0.0-dev-700c3a186e5 \n2023-04-07 12:59:26 \xe2\x9d\xa4\xef\xb8\x8f by Substrate DevHub <https://github.com/substrate-developer-hub>, 2017-2023 \n2023-04-07 12:59:26 Chain specification: Development \n2023-04-07 12:59:26 Node name: extra-large-arch-5066 \n2023-04-07 12:59:26 Role: AUTHORITY \n2023-04-07 12:59:26 Database: RocksDb at /var/folders/dy/1hb072zd0g1d5ppckgm48ml40000gn/T/substrateekR4s0/chains/dev/db/full \n2023-04-07 12:59:26 \xe2\x9b\x93 Native runtime: node-template-100 (node-template-1.tx1.au1) \nError: Service(Client(VersionInvalid("cannot deserialize module: UnknownOpcode(192)")))\n2023-04-07 12:59:26 Cannot create a runtime error=Other("cannot deserialize module: UnknownOpcode(192)")\nRun Code Online (Sandbox Code Playgroud)\n我在另一个页面看到我必须没有最新版本的 rustup,所以我目前的版本已于 2023/01/01 更新:
\n\xe2\x9e\x9c substrate-node-template git:(my-wip-branch) rustup toolchain list\nstable-aarch64-apple-darwin\nnightly-2021-03-14-aarch64-apple-darwin\nnightly-2022-03-14-aarch64-apple-darwin\nnightly-2022-06-30-aarch64-apple-darwin\nnightly-2023-01-01-aarch64-apple-darwin (default)\nRun Code Online (Sandbox Code Playgroud)\n … 我编写了一个宏,它在 Rust 中实现了类似 Scala 的理解。它将变成这样:
map_for!{
x <- 0..4;
y = 2*x;
z <- 0..1;
=> y+z
}
Run Code Online (Sandbox Code Playgroud)
进入这个:
((0..4).map (move |x| { let y = 2 * x; (x, y) }))
.flat_map (move |params| {
let (x, y) = params;
(0..1).map (move |z| { y + z })
})
Run Code Online (Sandbox Code Playgroud)
x这是可行的,但编译器会发出“未使用的变量”警告,因为flat_map. 我可以通过在宏中的语句#[allow(unused_variables)]之前添加来禁用警告,但随后它会删除所有未使用的变量警告,因此:let
map_for!{
x <- 0..4;
y = 2;
z <- 0..1;
=> y+z
}
Run Code Online (Sandbox Code Playgroud)
将扩展为:
((0..4).map (move |x| { let y …Run Code Online (Sandbox Code Playgroud) 在尝试回答这个问题时,我写了这段代码:
trait MyTrait {
type A;
type B;
}
trait WithFoo: MyTrait {
fn foo(a: Self::A) -> Self::B;
}
impl<T, U: MyTrait<A = T, B = T>> WithFoo for U {
fn foo(a: T) -> T {
a
}
}
struct S1;
impl MyTrait for S1 {
type A = u32;
type B = f32;
}
impl WithFoo for S1 {
fn foo<T>(a: Self::A) -> Self::B {
a as f32
}
}
struct S2;
impl MyTrait for S2 …Run Code Online (Sandbox Code Playgroud) DashMap像这样的代码在 Rust 中使用 a 会产生死锁吗?
// snippet_1
let a = DashMap::new();
let b = DashMap::new();
// thread1
for v in a.iter(){
xxx
}
for v in b.iter(){
xxx
}
//thread2
for v in b.iter(){
xxx
}
for v in a.iter(){
xxx
}
Run Code Online (Sandbox Code Playgroud)
// snippet_2
let a = DashMap::new();
let b = DashMap::new();
// thread1
for v in a.iter(){
xxx
}
for v in b.iter(){
xxx
}
//thread2
for v in b.iter(){
xxx
for v in a.iter() {
xxx
} …Run Code Online (Sandbox Code Playgroud) 是否可以使用定义为宏参数的字段在Rust宏内构建枚举?我试过这个:
macro_rules! build {
($($case:ty),*) => { enum Test { $($case),* } };
}
fn main() {
build!{ Foo(i32), Bar(i32, i32) };
}
Run Code Online (Sandbox Code Playgroud)
但它失败了 error: expected ident, found 'Foo(i32)'
请注意,如果在枚举内定义了字段,则没有问题:
macro_rules! build {
($($case:ty),*) => { enum Test { Foo(i32), Bar(i32, i32) } };
}
fn main() {
build!{ Foo(i32), Bar(i32, i32) };
}
Run Code Online (Sandbox Code Playgroud)
如果我的宏只接受简单字段,它也有效:
macro_rules! build {
($($case:ident),*) => { enum Test { $($case),* } };
}
fn main() {
build!{ Foo, Bar };
}
Run Code Online (Sandbox Code Playgroud)
但是我一直无法让它在一般情况下工作.
rust ×10
traits ×2
blockchain ×1
deadlock ×1
macros ×1
performance ×1
rust-macros ×1
substrate ×1