代码类似于以下内容,在函数中是Context结构的实现,定义如下:
struct Context {
lines: isize,
buffer: Vec<String>,
history: Vec<Box<Instruction>>,
}
Run Code Online (Sandbox Code Playgroud)
而功能,当然作为一种实现:
fn _execute_history(&mut self, instruction: &Instruction) -> Reaction {
let num = instruction.suffix.parse::<usize>();
match num {
Ok(number) => {
match self.history.get(number) {
Some(ins) => { return self.execute(*ins); },
_ => { /* Error handling */ }
}
}
Err(..) => { /* Error handling */ }
}
}
Run Code Online (Sandbox Code Playgroud)
这不编译,我不明白错误信息.我在网上搜索类似的问题,我似乎无法在这里解决问题.我来自Python背景.完整的错误:
hello.rs:72:23: 72:35 note: previous borrow of `self.history[..]` occurs here; the immutable
borrow prevents subsequent moves or mutable borrows …Run Code Online (Sandbox Code Playgroud) 我正在研究一个主要返回闭包而不是传统的基于类的方法的Python项目.例如:
def term(token):
def fn(text):
return (...)
return fn
Run Code Online (Sandbox Code Playgroud)
可以想象,调试和测试闭包是一个噩梦,特别是如果我在整个代码中使用相同的名称来封闭.例如:
>>> term('t')
<function fn at 0x...>
Run Code Online (Sandbox Code Playgroud)
所以我尝试将它包装在一个类中以__repr__从namedtuple中获取特殊处理:
def rr(cls, attrs):
T = namedtuple(cls, attrs)
class G(object):
__slots__ = ()
def __init__(self, repr_message, callable):
self.callable = callable
self.repr_message = repr_message
def __call__(self, *args, **kwargs):
return self.callable(*args, **kwargs)
def __repr__(self):
return self.repr_message
K = type(cls, (G,), {})
def wrapper(fn):
def init(*args, **kwargs):
t = T(*args, **kwargs)
return K(
repr(t),
fn(*args, **kwargs),
)
return init
return wrapper
Run Code Online (Sandbox Code Playgroud)
这样:
>>> rr('Term', ['token'])(term)('$') …Run Code Online (Sandbox Code Playgroud) 所以我有这个代码,它返回有关该文件的一系列信息.它返回两次输出,如第二个片段所示.
list = os.listdir(".")
print Style.BRIGHT + "size info last-accessed last-changed filename" + Style.RESET_ALL
for f in list:
info = os.stat(f)
# " size info last-accessed last-changed filename
print " ", info[ST_SIZE], " " + f
Run Code Online (Sandbox Code Playgroud)
并输出:
size info last-accessed last-changed filename
6243 .bash_history
55 .bash_profile
3 .CFUserTextEncoding
15364 .DS_Store
102 .fontconfig
105 .sh_history
102 .ssh
442 .Trash
0 .Xauthority
105 console
6688 console.py
272 Desktop
238 Documents
442 Downloads
2924 Folder
1156 Library
204 Movies
102 Music
136 Pictures …Run Code Online (Sandbox Code Playgroud)