我想将一个结构体的成员函数传递给另一个结构体。
对不起,英语不好,不能说更多细节。
use std::thread;
struct Struct1 {}
impl Struct1 {
pub fn do_some(&mut self, s: &str) {
// do something use s to modify self
}
}
struct Struct2 {
cb1: Box<Fn(&mut Struct1, &str)>,
}
fn main() {
let s1 = Struct1 {};
let s2 = Struct2 {
cb1: Box::new(s1.do_some), // how to store do_some function in cb1 ?
};
}
Run Code Online (Sandbox Code Playgroud) 我刚刚在Rust项目中添加了一个外部包:
[dependencies]
feed = "2.0"
Run Code Online (Sandbox Code Playgroud)
这个箱子有几个依赖关系,特别是openssl-sys v0.9.10.当我尝试构建我的项目时,这个失败了:
$ cargo build
Compiling unicode-normalization v0.1.4
Compiling openssl-probe v0.1.0
Compiling matches v0.1.4
Compiling log v0.3.7
Compiling unicode-bidi v0.2.5
Compiling libc v0.2.21
Compiling quick-xml v0.4.2
Compiling pkg-config v0.3.9
Compiling rss v0.4.0
Compiling idna v0.1.1
Compiling time v0.1.36
Compiling num-traits v0.1.37
Compiling gcc v0.3.45
Compiling num-integer v0.1.34
Compiling url v1.4.0
Compiling num-iter v0.1.33
Compiling num v0.1.37
Compiling chrono v0.3.0
Compiling openssl-sys v0.9.10
Compiling libz-sys v1.0.13
error: failed to run custom build command for …Run Code Online (Sandbox Code Playgroud) 我创建了一个"hello world"Rust应用程序cargo new.当我执行git status它时显示了一堆文件:
A rust/welcomec/Cargo.lock
A rust/welcomec/Cargo.toml
A rust/welcomec/src/main.rs
A rust/welcomec/target/debug/.cargo-lock
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/bin-welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/bin-welcome-2d68725c8fae6fd1.json
A rust/welcomec/target/debug/.fingerprint/welcomec-2d68725c8fae6fd1/dep-bin-welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/deps/welcome-2d68725c8fae6fd1
A rust/welcomec/target/debug/welcome
A rust/welcomec/target/debug/welcome.d
Run Code Online (Sandbox Code Playgroud)
我可以安全地忽略这些文件和/或目录吗?
我试图测量结构及其字段(Playground)的大小:
use std::mem;
struct MyStruct {
foo: u8,
bar: char,
}
println!("MyStruct: {}", mem::size_of::<MyStruct>());
let obj = MyStruct { foo: 0, bar: '0' };
println!("obj: {}", mem::size_of_val(&obj));
println!("obj.foo: {}", mem::size_of_val(&obj.foo));
println!("obj.bar: {}", mem::size_of_val(&obj.bar));
Run Code Online (Sandbox Code Playgroud)
该程序打印:
MyStruct: 8
obj: 8
obj.foo: 1
obj.bar: 4
Run Code Online (Sandbox Code Playgroud)
因此结构的大小大于其字段大小的总和(这将是5).这是为什么?
A Mech带有一个驱动程序,它是一个Named实体.在运行时,省略的Mech构造函数会查询要使用的特定类型驱动程序的外部源.
trait Named {
fn name(self) -> String;
}
struct Person {
first_name: String,
last_name: String
}
impl Named for Person {
fn name(self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
}
pub struct Mech<'a> {
driver: Box<Named + 'a>,
}
impl<'a> Mech<'a> {
pub fn driver_name(self) -> String {
self.driver.name()
}
}
Run Code Online (Sandbox Code Playgroud)
方法driver_name将所有权返回给a String,以便在链式调用中进一步使用(在实际代码中它是a Command).它编译失败:
error[E0161]: cannot move a value of type Named + 'a: the size of …Run Code Online (Sandbox Code Playgroud) 在尝试编写Conway的《人生游戏》时,我正在与借阅检查器进行搏斗。我有两个for循环,对进行可变借用self.cell_states,这是一个Vec<Vec<CellState>>(CellState是一个枚举),以便可以更新每个单元格的Alive或Dead状态。
为了确定一个细胞是应该存活还是死亡,它需要查看周围有多少个细胞存活。这是我遇到问题的地方。我可以检查单元格是否存在的唯一方法是执行一条match语句,但是显然match借用了该值,这是不允许的,因为我已经进行了可变借用。在我看来,我只能复制该值并对照复制的值进行检查,因此我尝试了match self.cell_states[i+x-1][j+y-1].clone() {...},但无济于事。不借钱怎么办比赛?
error[E0502]: cannot borrow `cell_states` as immutable because it is also borrowed as mutable
--> src/main.rs:18:27
|
11 | for (i, row) in cell_states.iter_mut().enumerate() {
| ----------------------------------
| |
| mutable borrow occurs here
| mutable borrow used here, in later iteration of loop
...
18 | match cell_states[i+x-1][j+y-1] {
| ^^^^^^^^^^^ immutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
我的代码如下(Playground):
error[E0502]: cannot …Run Code Online (Sandbox Code Playgroud) rustc foo.rs如果我没有安装C ++工具链,则使用编译Rust文件将失败。但是在编译Go程序时,不需要这样的工具链。这是为什么?
我不太明白为什么会这样编译:
fn main() {
let mut v = vec![1,2,3];
for i in 1..v.len() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
...而这不是:
fn main() {
let mut v = vec![1,2,3];
for (i,_) in v.iter().enumerate() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
fn main() {
let mut v = vec![1,2,3];
for i in 1..v.len() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我们都是不可变的借用(一个在调用时len(),另一个在调用时iter())。
因此,我的期望是不应该编译第一个代码段-当存在不可变的借位时,我们在进行赋值时会进行可变的借位。
我有什么误会?
阅读Rust教程我发现了以下代码片段:
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e)
};
Run Code Online (Sandbox Code Playgroud)
这似乎f可以具有type Result<_, _>(if f == Err(e))的值或类型的值_(即,无论何种类型file).
那么这是否意味着Rust中的匹配表达式是不确定的类型?
rust ×10
rust-cargo ×2
build-tools ×1
c++ ×1
go ×1
ownership ×1
rust-macros ×1
struct ×1
toolchain ×1
traits ×1