我可能没有正确描述我的问题标题,如果需要请编辑它.
我正在尝试为LXC库创建一个Rust接口,这是用C语言编写的.
我已成功调用简单函数lxc_get_version,lxc_container_new但我无法访问struct lxc_container块中描述的函数.
这是我的代码的一部分:
#[link(name = "lxc")]
extern {
// LXC part
fn lxc_get_version() -> *const c_char;
fn lxc_container_new(name: *const c_char, configpath: *const c_char) -> LxcContainer;
// LXC container parts
fn is_defined(container: &LxcContainer) -> bool;
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误:
note: test.o: In function `LxcContainer::is_defined::heb2f16a250ac7940Vba':
test.0.rs:(.text._ZN12LxcContainer10is_defined20heb2f16a250ac7940VbaE+0x3e): undefined reference to `is_defined'
Run Code Online (Sandbox Code Playgroud)
编辑:我已经管理了C结构中的函数称为函数指针.我试过谷歌像"Rust C函数指针",但没有运气.
我有一些像这样的代码:
$('.play').on( 'click', function(){
console.log('click');
});
Run Code Online (Sandbox Code Playgroud)
该.play元素是使用$('.game').html('<span class="paly">Play</span>')方法动态创建的.但是,当我点击此跨度时,我的控制台日志中没有任何内容.我究竟做错了什么?
谢谢.
PS:我正在使用jQuery 1.7.1
我正在尝试写一些基本的通用:
pub struct MyGeneric<T> {
vec: Vec<T>
}
impl<T> MyGeneric<T> {
fn add(&mut self, item: T) {
if !self.vec.contains(&item) {
self.vec.push(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但得到一个错误:
priority_set.rs:23:10: 23:35 error: the trait `core::cmp::PartialEq` is not implemented for the type `T`
priority_set.rs:23 if !self.vec.contains(&item) {
^~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
我试图以几种方式实现PartialEq查看API文档,但我自己找不到解决方案.我对特质概念不太熟悉,所以我需要帮助.
谢谢.
我有一个类是标准dict的子类:
class Result(dict):
""" Dict-like object with special methods """
def content(self):
return self.__getitem__('_content')
def attrs(self):
return self.__getitem__('_attrs')
Run Code Online (Sandbox Code Playgroud)
此对象中的示例表示:
{
'_attrs': {'id': 1},
'description': 'testtest',
'calories': 1234,
'_content': 'Sample content',
'name': 'qwerty',
'price': 12390
}
Run Code Online (Sandbox Code Playgroud)
我希望我的类在迭代时跳过带有下划线键的记录.
# data is Result()
>>> for item in data:
>>> print(item)
'description'
'calories'
'name'
'price'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
更新: 除了正确的答案,我还覆盖了keys()和items()方法来隐藏下划线键,即使这些方法将在迭代中使用:
def __iter__(self):
for key in self.keys():
yield key
def keys(self):
return [key for key in super().keys() if key not in ['_attrs', '_content']]
def items(self):
return [(key, …Run Code Online (Sandbox Code Playgroud) 我有一个包含7,200,000个元素的列表.当我尝试使用set()转换它时,它会减少到4,500,000个元素.我该怎么做才能绕过这个问题?
我在Windows上使用Python 3.2.2 x86.