我遇到了这行用 Rust 编写的代码,不明白这里发生了什么。谁能解释一下这行用 Rust 编写的代码吗?
let decimal = 65.4321_f32;
Run Code Online (Sandbox Code Playgroud)
有什么_f32作用?
提前致谢 :)
我手动定义一个结构并为其MyData实现PartialEq和特征。Hash
我定义了一个枚举,其中包括Rc<MyData>和Rc<RefCell<MyData>>。
我想要为枚举导出PartialEqand ,但失败了:Hash
PartialEq都Hash适用于Rc<MyData>;PartialEq于Rc<RefCell<MyData>>;Hash不起作用Rc<RefCell<MyData>>!我有两个问题:
为什么?为什么只有Hash不起作用Rc<RefCell<MyData>>?
如何修复它?
我无法Hash实施Rc<RefCell<MyData>>. 经过搜索后,我找到了一种方法:定义一个新的包装结构,例如struct RRWrapper<T> (Rc<RefCell<T>>),然后实现Hashthis RRWrapper。但这会带来很多代码。有惯用的方法吗?我认为这是一般用法。
预先感谢,
吴
Rc<RefCell<MyData>>PS:在我程序的实际代码中,只有枚举中有,但没有Rc<MyData>。我放在Rc<MyData>这里只是为了比较。Rc<RefCell<T>>PS2:在我的程序的实际代码中,枚举中有多个。
原始源代码:
use std::rc::Rc;
use std::cell::RefCell;
use std::hash::{Hash, Hasher};
struct MyData {
i: i64,
}
impl Hash …Run Code Online (Sandbox Code Playgroud) 如何将 const 或 static 传递给 Rust 中的函数?
为什么这些不起作用?:
const COUNT: i32 = 5;
fn main() {
let repeated = "*".repeat(COUNT);
println!("Repeated Value: {}", repeated);
}
Run Code Online (Sandbox Code Playgroud)
和
static COUNT: i32 = 5;
fn main() {
let repeated = "*".repeat(COUNT);
println!("Repeated Value: {}", repeated);
}
Run Code Online (Sandbox Code Playgroud)
他们回来了:
mismatched types
expected `usize`, found `i32`
Run Code Online (Sandbox Code Playgroud)
但这些工作正常吗?:
fn main() {
let repeated = "*".repeat(5);
println!("Repeated Value: {}", repeated);
}
Run Code Online (Sandbox Code Playgroud)
和
fn main() {
let count = 5;
let repeated = "*".repeat(count);
println!("Repeated Value: {}", repeated);
} …Run Code Online (Sandbox Code Playgroud) 在 Rust 1.58 中,println!("{x}");支持(格式字符串中捕获的标识符),但我无法打印结构,因为我没有指定{:?}. 有什么方法可以用 new 显示结构吗println!?
#[derive(Debug)]
struct Structure {
name: String,
version: u32
}
fn main() {
let structure = Structure { name: "name".to_string(), version: 1 };
println!("{:?}", structure); // working
println!("{structure}"); // not working
}
Run Code Online (Sandbox Code Playgroud) 在 C++ 中,我们可以轻松地对向量的一部分进行排序
排序(A.begin()+开始,A.begin()+结束);
但在 Rust 中我找不到好的方法,有人可以帮助我吗?
类型是什么
let plus:Plus = |x| move |y| x + y;
Run Code Online (Sandbox Code Playgroud)
明显地,
type Plus = fn(isize) -> fn(isize) -> isize;
Run Code Online (Sandbox Code Playgroud)
不会工作,因为最后一部分不是函数指针而是闭包。
来自这里的Python。
我想知道为什么 BTreeMap 是可散列的。我并不惊讶 Hashmap 不是,但我不明白为什么 BTreeMap 是。
例如,我可以这样做:
let mut seen_comb: HashSet<BTreeMap<u8, u8>> = HashSet::new();
seen_comb.insert(BTreeMap::new());
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
let mut seen: HashSet<HashMap<u8, u8>> = HashSet::new();
seen.insert(HashMap::new());
Run Code Online (Sandbox Code Playgroud)
因为我得到:
error[E0599]: the method `insert` exists for struct `HashSet<HashMap<u8, u8>>`, but its trait bounds were not satisfied
--> src/main.rs:14:10
|
14 | seen.insert(HashMap::new());
| ^^^^^^ method cannot be called on `HashSet<HashMap<u8, u8>>` due to unsatisfied trait bounds
|
::: /home/djipey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:209:1
|
209 | pub struct HashMap<K, V, S = RandomState> {
| ----------------------------------------- doesn't …Run Code Online (Sandbox Code Playgroud) 根据文档,
\n\n\n数据竞争会导致未定义的行为,并且当您\xe2\x80\x99尝试在运行时追踪它们时,可能很难诊断和修复;Rust 通过拒绝编译带有数据竞争的代码来防止这个问题!
\n
除非你\xe2\x80\x99re多线程,否则为什么这是一个问题?如果同时访问一个变量及其一个mut引用,\xe2\x80\x99t 这个问题是否仍然存在?
我有以下代码:
use std::collections::HashMap;
fn doublez(h1: &HashMap<String, i32>, h2: &HashMap<String, i32>) {
dbg!(h1, h2);
}
fn main() {
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let teams = vec![
String::from("Blue"),
String::from("Yellow"),
];
let initial_scores = vec![10, 50];
let team_scores: HashMap<_, _> = teams.into_iter().zip(initial_scores.into_iter()).collect();
let mut ts2 = &team_scores;
let mut ts3 = &team_scores;
doublez(ts2, ts3);
}
Run Code Online (Sandbox Code Playgroud)
我正在试验 Rust 所有权规则,并且正在测试不能拥有多个可变引用的整个想法,但在这段代码中,我以 ts2 和 ts3 的形式对 team_scores 哈希图进行了两个可变引用,但是对于不管什么原因,代码编译得很好。这是为什么?
我在正确理解 Rust 中的生命周期概念时遇到了一些麻烦。特别是在涉及结构的对象本身和结构的成员时。
以下最小示例显示了我遇到的一个问题:
use redis::{Connection, PubSub};
pub struct Db<'a> {
conn: Connection,
pubsub: Option<PubSub<'a>>
}
impl<'a> Db<'a> {
fn open(address: &str) -> Self {
let client = redis::Client::open(address).unwrap();
let conn = client.get_connection().unwrap();
Self {conn, pubsub: None}
}
fn subscribe(&'a mut self) {
let pubsub = self.conn.as_pubsub();
self.pubsub = Some(pubsub)
}
}
fn main() {
let mut db = Db::open("localhost");
db.subscribe();
}
Run Code Online (Sandbox Code Playgroud)
编译结束并出现以下错误:
error[E0597]: `db` does not live long enough
--> src/main.rs:22:5
|
22 | db.subscribe();
| ^^^^^^^^^^^^^^ borrowed value …Run Code Online (Sandbox Code Playgroud)