basic/collections/hashmap #780
Replies: 22 comments 27 replies
-
本篇还漏了一个很重要的知识点, |
Beta Was this translation helpful? Give feedback.
-
理想很骨感,现实很丰满 |
Beta Was this translation helpful? Give feedback.
-
国足牛逼!!! (这,这,脸都…… |
Beta Was this translation helpful? Give feedback.
-
如果发生哈希碰撞了,会发生什么? |
Beta Was this translation helpful? Give feedback.
-
例如 |
Beta Was this translation helpful? Give feedback.
-
第二段代码为什么team.0加了借用,team.1没加? |
Beta Was this translation helpful? Give feedback.
-
"SipHash 在中等大小的 Key 上,性能相当不错,但是对于小型的 Key (例如整数)或者大型 Key (例如字符串)来说,性能还是不够好" 大中小有评判的标准吗? |
Beta Was this translation helpful? Give feedback.
-
fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
HashMap 是线程安全的吗 |
Beta Was this translation helpful? Give feedback.
-
课后练习第二题第一个方案跑不通, let teams = vec![...]; 加了vec!就可以了 |
Beta Was this translation helpful? Give feedback.
-
好家伙,直接对value修改,够暴力。 |
Beta Was this translation helpful? Give feedback.
-
let a: Option<i32> = Some(5);
let b = a.copy(); // b is now Some(5)
copy 和 copied最大区别是一个是Some()一个是Ok() let x: Option<i32> = Some(5);
let y: i32 = x.unwrap(); // y is now 5
let z: Result<i32, &str> = Ok(10);
let w: i32 = z.unwrap(); // w is now 10
let x: Option<i32> = None;
let y: i32 = x.unwrap_or(10); // y is now 10
let z: Result<i32, &str> = Err("Error message");
let w: i32 = z.unwrap_or(20); // w is now 20 unwrap()就是要么给我结果要么panic,unwrap_or()给你一个default值,前者不推荐使用 let x: Option<i32> = None;
let y: i32 = x.unwrap_or_else(|| {
// Some complex computation that returns a default value
100
}); // y is now 100
let z: Result<i32, &str> = Err("Error message");
let w: i32 = z.unwrap_or_else(|| {
// Some complex computation that returns a default value
200
}); // w is now 200 unwrap_or_else()你可以写一个闭包函数 |
Beta Was this translation helpful? Give feedback.
-
通过循环遍历键值对的方式这个地方的打印结果反了。
应该为
|
Beta Was this translation helpful? Give feedback.
-
练习2的collection解法为什么会报错,没看懂
use std::collections::HashMap;
fn main() {
let teams = [
("Chinese Team", 100),
("American Team", 10),
("France Team", 50),
];
let mut teams_map1 = HashMap::new();
for team in &teams {
teams_map1.insert(team.0, team.1);
}
let teams_map2: HashMap<_,_> = teams.into_iter().collect();
assert_eq!(teams_map1, teams_map2);
println!("Success!")
}
|
Beta Was this translation helpful? Give feedback.
-
let red = scores.entry(String::from("Red")).or_insert(0); |
Beta Was this translation helpful? Give feedback.
-
"好在我们有哈希函数:通过它把 Key 计算后映射为哈希值,然后使用该哈希值来进行存储、查询、比较等操作。 但是问题又来了,如何保证不同 Key 通过哈希后的两个值不会相同?如果相同,那意味着我们使用不同的 Key,却查到了同一个结果,这种明显是错误的行为。 此时,就涉及到安全性跟性能的取舍了。" 似乎作者理解的不太对,HashMap的Hash算法不需要保证“不同Key通过Hash后值不相同”。“不同Key的Hash相同”就是Hash碰撞,是HashMap的正常情况。 Hash碰撞不可能避免:Value有无限种可能;而64位Hash值也只能表达2^64种情况。 |
Beta Was this translation helpful? Give feedback.
-
关于Option中的copied和cloned方法的区别,可以参考源码实现 #[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "copied", since = "1.35.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn copied(self) -> Option<T>
where
T: Copy,
{
// FIXME: this implementation, which sidesteps using `Option::map` since it's not const
// ready yet, should be reverted when possible to avoid code repetition
match self {
Some(&v) => Some(v),
None => None,
}
}
/// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
/// option.
///
/// # Examples
///
/// ```
/// let x = 12;
/// let opt_x = Some(&x);
/// assert_eq!(opt_x, Some(&12));
/// let cloned = opt_x.cloned();
/// assert_eq!(cloned, Some(12));
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn cloned(self) -> Option<T>
where
T: Clone,
{
match self {
Some(t) => Some(t.clone()),
None => None,
}
} |
Beta Was this translation helpful? Give feedback.
-
在 Rust 中,
|
Beta Was this translation helpful? Give feedback.
-
是的,在 Rust 的 这里是一个具体的例子来说明这个行为: use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
// 插入键为 "key1" 的值 "value1"
map.insert("key1", "value1");
println!("{:?}", map); // 输出: {"key1": "value1"}
// 再次插入键为 "key1" 的新值 "value2"
let old_value = map.insert("key1", "value2");
println!("{:?}", map); // 输出: {"key1": "value2"}
println!("{:?}", old_value); // 输出: Some("value1")
} 在这个例子中,第二次调用 这种行为使得 |
Beta Was this translation helpful? Give feedback.
-
请问一下哈希函数和 dos 攻击有什么联系 |
Beta Was this translation helpful? Give feedback.
-
teams_list.into_iter().collect() |
Beta Was this translation helpful? Give feedback.
-
basic/collections/hashmap
https://course.rs/basic/collections/hashmap.html
Beta Was this translation helpful? Give feedback.
All reactions