rust

Using enums for dynamic polymorphism in Rust

旧时模样 提交于 2020-11-28 08:28:06
问题 When one already knows all the finite number of types involved in some code which needs dynamic polymorphism, using enum can be better for performances compared to using Box since the latter uses dynamic memory allocation and you'll need to use trait objects which have virtual function call as well. That said, compared to the equivalent code in C++ using std::variant and std::visit , looks like Rust in this scenario has more boilerplate coding involved, at least for me (I have not yet learned

What is the proper way to create a new generic struct?

纵饮孤独 提交于 2020-11-28 07:55:49
问题 I'm trying to make a generic struct that can be initialized to something of type T . It looks like this: pub struct MyStruct<T> { test_field: Option<T>, name: String, age: i32, } impl MyStruct<T> { fn new(new_age: i32, new_name: String) -> MyStruct<T> { MyStruct<T> { test_field: None, age: new_age, name: new_name, } } } This doesn't seem to work. Among other errors, I get: error: chained comparison operators require parentheses --> src/lib.rs:9:17 | 9 | MyStruct<T> { | ^^^^^ | 回答1: I highly

What is the proper way to create a new generic struct?

一世执手 提交于 2020-11-28 07:54:12
问题 I'm trying to make a generic struct that can be initialized to something of type T . It looks like this: pub struct MyStruct<T> { test_field: Option<T>, name: String, age: i32, } impl MyStruct<T> { fn new(new_age: i32, new_name: String) -> MyStruct<T> { MyStruct<T> { test_field: None, age: new_age, name: new_name, } } } This doesn't seem to work. Among other errors, I get: error: chained comparison operators require parentheses --> src/lib.rs:9:17 | 9 | MyStruct<T> { | ^^^^^ | 回答1: I highly

Is there a way to initialize an empty slice?

笑着哭i 提交于 2020-11-28 07:02:07
问题 Something like this? [String, 0] Vec::new() is not an option. 回答1: This creates an empty array: let thing: [String; 0] = []; You can also get a slice from the array: let thing: &[String] = &[]; You can also use as : some_function([] as [String; 0]); some_function(&[] as &[String]); 来源: https://stackoverflow.com/questions/45533699/is-there-a-way-to-initialize-an-empty-slice

Is there a way to initialize an empty slice?

时光总嘲笑我的痴心妄想 提交于 2020-11-28 07:01:08
问题 Something like this? [String, 0] Vec::new() is not an option. 回答1: This creates an empty array: let thing: [String; 0] = []; You can also get a slice from the array: let thing: &[String] = &[]; You can also use as : some_function([] as [String; 0]); some_function(&[] as &[String]); 来源: https://stackoverflow.com/questions/45533699/is-there-a-way-to-initialize-an-empty-slice

深度剖析,为何C语言在开发领域的地位如此稳固

心不动则不痛 提交于 2020-11-27 20:52:55
C语言 在这过去的五十年间, 已经 逐渐发展 成为 极其 重要的软件开发语言。这里 简要讲述一下在现在, 它 将 如何与C++、Java、C#、Go、Rust和Python进行竞争 并保持一定优势 。 对于计算机编程语言来说,没有什么技术能 沿 用 半个世纪 年,除非它比其他的都好用。C语言于 上世纪七十年代初 年面世,如今在软件世界仍保持着底层 基础 开发的主流语言的地位。 有时 , 一个技术能被长久的流传使用,是因为人们还没有找到一个更好的替代品。在过去的几十年,涌现了大量的语言—— 甚至出现 专门为了挑战C语言的统治地位而设计 的新语言 。 C真的很难被替代。编程语言调查和软件开发实例都印证了可以用远比 使用 C 语言更好 的方式来做 开发 。但C的地位仍岿然不动, 因为 它的背后是几十年的 积淀和进步 。几乎没有语言可以在性能 、 逻辑、 或者普遍性上打败它。 C vs. C++ 很自然地, 人们会拿 C与C++做对比, 顾名思义 出,C++是从C发展而来的。两者之间的不同就在于易扩展性,或者易用性。 语法和方式上,C++与C语言比较接近,但C++提供了很多原生C没有的有用特性:命名空间、模板、异常、内存管理。项目如果对于性能比较敏感,例如数据库和机器学习,通常使用C++编写 会对提供 系统提高性能 更有帮助 。 除此之外,C++比C更容易扩展。C+

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

北慕城南 提交于 2020-11-26 13:28:31
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

蹲街弑〆低调 提交于 2020-11-26 13:21:52
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

天涯浪子 提交于 2020-11-26 13:20:40
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

How do I debug a failing cargo test in GDB?

余生颓废 提交于 2020-11-25 20:55:06
问题 I have a failing Cargo test: $ cargo test [snip] Running target/gunzip-c62d8688496249d8 running 2 tests test test_extract_failure ... FAILED test test_extract_success ... ok failures: ---- test_extract_failure stdout ---- task 'test_extract_failure' panicked at 'assertion failed: result.is_err()', /home/dhardy/other/flate2-rs/tests/gunzip.rs:19 failures: test_extract_failure test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured task '<main>' panicked at 'Some tests failed', /home