我有一个问题要问。我们看一下下面的代码:
use std::collection::BTreeMap;
fn main() {
let mut hm: BTreeMap<String, String> = BTreeMap::new();
hm.insert("asdf".to_owned(), "zxcv".to_owned());
println!("{:?}", hm.get("asdf"));
}
Run Code Online (Sandbox Code Playgroud)
因此,尽管BTreeMapkeep Strings 它接受其他可以与键类型进行比较的类型。
但这不仅仅是 的情况Vec<T>。因此,下面的代码将是一个错误:
fn main() {
let v: Vec<String> = vec!["hello".to_owned()];
println!("{:?}", v.binary_search("hello"));
}
Run Code Online (Sandbox Code Playgroud)
该代码片段将无法编译,因为binary_search调用了对所提供值的完全相同类型的引用。我有点困惑为什么。
问题是我不能强迫gnu as将其解释jmp为shortor near,它不断地将其解释为far。
例如,以下代码会导致segfault:
int main() {
asm volatile (
// ".intel_syntax noprefix\n\t"
// "jmp lbl%=\n\t"
// "lbl%=:\n\t"
"jmp *lbl%=(%%rip)\n\t"
"lbl%=:\n\t"
// ".att_syntax\n\t"
: : : );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
切换到注释的.intel_syntax变体,它工作得很好。
唯一的区别是
jmp lbl%=变成inteleb 00
尽管
jmp *lbl%=(%%rip)变成attff 25 00 00 00 00
.att_syntax如果将其解释为 ,如何强制它short jump?
考虑以下示例:
pub enum DigitalWallet {
WithMemo {
currency: String,
address: String,
tag: String,
},
WithoutMemo {
currency: String,
address: String,
},
}
impl<'a> DigitalWallet {
fn getCurrency(self: &'a Self) -> &'a String {
match self {
DigitalWallet::WithMemo {
currency: String, ..
} => currency,
DigitalWallet::WithoutMemo {
currency: String, ..
} => currency,
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会出现以下情况?
pub enum DigitalWallet {
WithMemo {
currency: String,
address: String,
tag: String,
},
WithoutMemo {
currency: String,
address: String,
},
}
impl<'a> DigitalWallet { …Run Code Online (Sandbox Code Playgroud) 在不包含要绑定的字段的情况下,如何为绑定提供默认文字值?
#[derive(Debug)]
struct Descriptor {
name: String,
note: Option<String>,
}
fn func2(
(d @ Descriptor {
note: (Some(note_content) | None),
..
}): &Descriptor,
) {
}
Run Code Online (Sandbox Code Playgroud)
note_content这不会编译,因为在这种情况下没有绑定None。我想指定一个,但我不知道正确的语法。
这并不能解决问题:
Some(note_content) | None, note_content @ "default literal".to_string()
Run Code Online (Sandbox Code Playgroud)
那里应该写什么?