rust

Why are multiple mutable borrows possible in the same scope?

好久不见. 提交于 2020-12-09 04:44:25
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Why are multiple mutable borrows possible in the same scope?

烂漫一生 提交于 2020-12-09 04:42:10
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Why are multiple mutable borrows possible in the same scope?

落爺英雄遲暮 提交于 2020-12-09 04:42:09
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Opposite of Borrow trait for a Copy type in Rust?

丶灬走出姿态 提交于 2020-12-09 03:49:52
问题 In Rust, I've seen the Borrow trait used to define functions that accept both an owned type or a reference, e.g. T or &T . The borrow() method is then called in the function to obtain &T . Is there some trait that allows the opposite (i.e. a function that accepts T or &T and obtains T ) for Copy types? E.g. for this example: use std::borrow::Borrow; fn foo<T: Borrow<u32>>(value: T) -> u32 { *value.borrow() } fn main() { println!("{}", foo(&5)); println!("{}", foo(5)); } This calls borrow() to

使用PHP的FFI调用cjieba分词的动态库

心不动则不痛 提交于 2020-12-09 00:25:25
选用CJieba的原因是FFI使用的是C的调用约定,如果用Cpp,还得自己包装一下,然后extern C,让编译器生成标准C的动态库。 碰到的问题 段错误 C变量没有初始化 直接调用了C的函数,没有通过FFI 初始化后的的C对象调用 非空判断 需要使用 FFI::isNull($x) 指针形式的数组 不能用foreach 指针形式数组的循环 查看C代码发现Cut部分如下: CJiebaWord* Cut(Jieba handle, const char* sentence, size_t len) { cppjieba::Jieba* x = (cppjieba::Jieba*)handle; vector words; string s(sentence, len); x->Cut(s, words); CJiebaWord* res = (CJiebaWord*)malloc(sizeof(CJiebaWord) * (words.size() + 1)); size_t offset = 0; for (size_t i = 0; i < words.size(); i++) { res[i].word = sentence + offset; res[i].len = words[i].size(); offset += res[i].len; } if (offset !

What is a function for structs like Java's instanceof?

送分小仙女□ 提交于 2020-12-08 20:11:24
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

冷暖自知 提交于 2020-12-08 20:07:47
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

What is a function for structs like Java's instanceof?

寵の児 提交于 2020-12-08 20:05:04
问题 I'm making an OOP chat client in Rust. The module messages.rs creates and handles messages to other modules as structs: SimpleMessage and ComplexMessage structs: //! # Messages use time::SteadyTime; /// Represents a simple text message pub struct SimpleMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, pub content: &'a str, } /// Represents attachments, like text or multimedia files. pub struct ComplexMessage<'a> { pub user: ... pub time: &'a SteadyTime<'a>, //pub content: PENDING }

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

人盡茶涼 提交于 2020-12-08 16:19:07
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

喜夏-厌秋 提交于 2020-12-08 16:18:06
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below