Tag: 生锈

什么是在Rust 1.x中读写文件的事实上的方式?

随着Rust相对较新,我看到了太多的读写文件的方式。 许多人是非常杂乱的片段,有人提出他们的博客,我发现的例子(即使在堆栈溢出)的99%是来自不稳定的构build,不再工作。 既然Rust是稳定的,那么读取或写入文件的简单,可读,非恐慌的片段是什么呢? 这是我读到的文本文件中最接近的东西,但它仍然没有编译,即使我相当肯定,我已经包括了我应该有的一切。 这是基于我在Google+上发现的所有地方的一个片段,我唯一改变的是旧的BufferedReader现在只是BufReader : use std::fs::File; use std::io::BufReader; use std::path::Path; fn main() { let path = Path::new("./textfile"); let mut file = BufReader::new(File::open(&path)); for line in file.lines() { println!("{}", line); } } 编译器抱怨: error: the trait bound `std::result::Result<std::fs::File, std::io::Error>: std::io::Read` is not satisfied [–explain E0277] –> src/main.rs:7:20 |> 7 |> let mut file = […]

Rust中的习惯性callback

在C / C ++中,我通常使用普通的函数指针进行callback,也许会传递一个void* userdata参数。 像这样的东西: typedef void (*Callback)(); class Processor { public: void setCallback(Callback c) { mCallback = c; } void processEvents() { for (…) { … mCallback(); } } private: Callback mCallback; }; 在Rust里做这件事的惯用方法是什么? 具体来说,我的setCallback()函数应该mCallback什么types, mCallback应该是什么types? 它应该采取一个Fn ? 也许FnMut ? 我把它保存Boxed ? 一个例子将是惊人的。

什么是与C ++的上下文单一化?

Dave Herman最近在Rust 的演讲中表示,他们从C ++中借用了这个属性。 我找不到这个话题。 有人可以解释一下单态是什么意思吗?

是否有可能在Rust中使用全局variables?

我知道,一般来说,全局variables是可以避免的。 尽pipe如此,我认为从实际意义上讲,使用它们有时是可取的(在variables是程序中不可或缺的部分)。 为了学习Rust,我正在使用sqlite3和GitHub上的Rust / sqlite3包编写一个数据库testing程序。 因此,必须(在我的testing程序中)(作为全局variables的一种替代方式),在大约十几个函数之间传递数据库variables。 下面是一个例子。 在Rust中使用全局variables是否可行也是可行的? 鉴于下面的例子,我可以声明和使用全局variables吗? extern crate sqlite; fn main() { let db: sqlite::Connection = open_database(); if !insert_data(&db, insert_max) { return; } } 我尝试了以下,但它似乎不是很正确,并导致下面的错误(我也尝试了一个unsafe块): extern crate sqlite; static mut DB: Option<sqlite::Connection> = None; fn main() { DB = sqlite::open("test.db").expect("Error opening test.db"); println!("Database Opened OK"); create_table(); println!("Completed"); } // Create Table fn […]

将模块分成几个文件

我想要一个有多个结构的模块, 每个模块都有自己的文件。 以Math模块为例: Math/ Vector.rs Matrix.rs Complex.rs 我希望每个结构都在同一个模块中,我将使用我的主文件,如下所示: use Math::Vector; fn main() { // … } 然而,Rust的模块系统(开始时有点混乱)没有提供一个明显的方法来做到这一点。 它似乎只允许你在一个文件中有你的整个模块。 这是不是土气? 如果没有,我该怎么做?

如何访问命令行参数?

Rust教程没有解释如何从命令行获取参数。 在所有示例中, fn main()仅显示一个空参数列表。 从main访问命令行参数的正确方法是什么?

Rust为什么需要明确的生命周期?

我正在阅读Rust书的有生之年的一章 ,并且我在这个例子中看到了一个命名/明确的生命周期: struct Foo<'a> { x: &'a i32, } fn main() { let x; // -+ x goes into scope // | { // | let y = &5; // —+ y goes into scope let f = Foo { x: y }; // —+ f goes into scope x = &f.x; // | | […]

如何在Rust中获得一个数组?

我有一个未知大小的数组,我想获得该数组的切片,并将其转换为一个静态大小的数组: fn pop(barry: &[u8]) -> [u8; 3] { barry[0..3] // mismatched types: expected `[u8, ..3]` but found `&[u8]` } 我将如何做到这一点?

generics结构的构造函数中的“期望types参数”错误

我正在尝试将活塞纹理存储在结构中。 struct TextureFactory<R> where R: gfx::Resources { block_textures: Vec<Rc<Texture<R>>>, } impl<R> TextureFactory<R> where R: gfx::Resources { fn new(window: PistonWindow) -> Self { let texture = Rc::new(gfx_texture::Texture::from_path( &mut *window.factory.borrow_mut(), "assets/element_red_square.png", Flip::None, &TextureSettings::new() ).unwrap()); let block_textures = Vec::new(); block_textures.push(texture); TextureFactory { block_textures: block_textures, } } } 这不会编译: src/main.rs:37:9: 39:10 error: mismatched types: expected `TextureFactory<R>`, found `TextureFactory<gfx_device_gl::Resources>` (expected […]

为什么我不能在同一个结构中存储一个值和一个对这个值的引用?

我有一个价值,我想存储这个价值,并在我的types的价值内引用的价值: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } 有时,我有一个价值,我想存储这个值,并在同一结构中存储对这个值的引用: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new(); Combined(thing, &thing) } 有时,我甚至没有参考价值,我得到了同样的错误: struct Combined<'a>(Parent, Child<'a>); fn make_combined<'a>() -> Combined<'a> { let parent […]