rust

How to borrow the T from a RefCell<T> as a reference?

孤者浪人 提交于 2020-02-03 15:24:26
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

Pattern matching on slices

大城市里の小女人 提交于 2020-02-03 13:24:48
问题 I did something like this, which works: let s = " \"".as_bytes(); let (space, quote) = (s[0], s[1]); I wanted to do something like this &[space, quote] = " \"".as_bytes(); But it gives me the error slice pattern syntax is experimental (see issue #23121) Is it possible to do something similar? 回答1: As the error tells you, slice pattern syntax is experimental. This means either the semantics are not clear or the syntax might change in the future. As a result, you need a nightly version of the

Is it possible to create a Stream from a File rather than loading the file contents into memory?

别来无恙 提交于 2020-02-03 11:09:40
问题 I'm currently using the rusoto_s3 lib to upload a file to S3. All the examples I have found do the same thing: Open a file, read the full contents of the file into memory ( Vec<u8> ), then convert the Vec into a ByteStream (which implements From<Vec<u8>> ). Here is a code example: fn upload_file(&self, file_path: &Path) -> FileResult<PutObjectOutput> { let mut file = File::open(file_path)?; let mut file_data: Vec<u8> = vec![]; file.read_to_end(&mut file_data)?; let client = S3Client::new

What happens on the stack when one value shadows another in Rust? [duplicate]

这一生的挚爱 提交于 2020-02-03 10:36:50
问题 This question already has answers here : Does Rust free up the memory of overwritten variables? (2 answers) In Rust, what's the difference between “shadowing” and “mutability”? (1 answer) Closed 8 months ago . I am reading Mastering Rust. There is an exercise at the end of the first chapter where sample code is provided, and the task is to fix it, iterating by using the generally quite helpful compiler error messages. I was expecting that the following was an error but it is not : for line in

Get actual path symlink is pointing to

别等时光非礼了梦想. 提交于 2020-02-03 10:35:10
问题 Given a symlink, I want to know to which directory this symlink is pointing. Does the Rust standard library offer something to find this out? I have only found unstable API functions so far. 回答1: It seems to me that you want fs::read_link: Reads a symbolic link, returning the file that the link points to. 回答2: You can use std::fs::canonicalize: Returns the canonical, absolute form of a path with all intermediate components normalized and symbolic links resolved. 来源: https://stackoverflow.com

How to convert float to binary without using unsafe code?

丶灬走出姿态 提交于 2020-02-03 09:53:41
问题 Is there a way to convert a floating point number ( f32 or f64 ) to a data type that I can access bitwise, like u32 / u64 ? That is, something corresponding to: fn as_bits(i: f64) -> u64 { unsafe { mem::transmute(i) } } but without the unsafe . This code is safe per the rules, even though it may not return the same values on all platforms, specifically for NaNs. The reverse safe interface would also be nice. 回答1: Rust 1.20 introduced f64::to_bits and f32::to_bits: fn main() { println!("{}",

Why do I get the error FromIterator<&{integer}> is not implemented for Vec<i32> when using a FlatMap iterator?

我的未来我决定 提交于 2020-02-03 08:03:54
问题 Consider this snippet: fn main() { let arr_of_arr = [[1, 2], [3, 4]]; let res = arr_of_arr .iter() .flat_map(|arr| arr.iter()) .collect::<Vec<i32>>(); } The compiler error is: error[E0277]: the trait bound `std::vec::Vec<i32>: std::iter::FromIterator<&{integer}>` is not satisfied --> src/main.rs:6:10 | 6 | .collect::<Vec<i32>>(); | ^^^^^^^ a collection of type `std::vec::Vec<i32>` cannot be built from an iterator over elements of type `&{integer}` | = help: the trait `std::iter::FromIterator<

How do I convert a boolean to an integer in Rust?

爷,独闯天下 提交于 2020-02-03 05:45:27
问题 How do I convert a boolean to an integer in Rust? As in, true becomes 1, and false becomes 0. 回答1: Cast it: fn main() { println!("{}", true as i32) } 回答2: A boolean value in Rust is guaranteed to be 1 or 0: The bool represents a value, which could only be either true or false . If you cast a bool into an integer, true will be 1 and false will be 0. A boolean value, which is neither 0 nor 1 is undefined behavior: A value other than false ( 0 ) or true ( 1 ) in a bool. Therefore, you can just

Using str and String interchangably

喜你入骨 提交于 2020-02-03 04:46:47
问题 Suppose I'm trying to do a fancy zero-copy parser in Rust using &str , but sometimes I need to modify the text (e.g. to implement variable substitution). I really want to do something like this: fn main() { let mut v: Vec<&str> = "Hello there $world!".split_whitespace().collect(); for t in v.iter_mut() { if (t.contains("$world")) { *t = &t.replace("$world", "Earth"); } } println!("{:?}", &v); } But of course the String returned by t.replace() doesn't live long enough. Is there a nice way

How to import a module from a directory outside of the `src` directory?

一个人想着一个人 提交于 2020-02-03 01:43:26
问题 I'm stuck when learning how to access a module. I'm trying to insert a folder other than src into src . It's not working and it gives me an error. Here this is my project tree. $ Project1 . |-- src | |-- main.rs | |--FolderinSrcFolder | |--folderinsrcmodule.rs | |--anothersrc | |--mod.rs | |-- rootmodule.rs |-- Cargo.toml |-- Cargo.lock How can I access anothersrc/mod.rs src/main.rs ? How can I access rootmodule.rs from src/main.rs ? I already read the Rust documentation. 回答1: Don't . Put all