rust

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

我与影子孤独终老i 提交于 2021-02-18 20:59:58
问题 &mut T and &mut T results in a compilation error; this is great, it's objectively wrong to borrow mutably twice. Is *mut T and *mut T undefined behaviour or is this a perfectly valid thing to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

♀尐吖头ヾ 提交于 2021-02-18 20:59:13
问题 &mut T and &mut T results in a compilation error; this is great, it's objectively wrong to borrow mutably twice. Is *mut T and *mut T undefined behaviour or is this a perfectly valid thing to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

How to opt out of running a doc test?

*爱你&永不变心* 提交于 2021-02-18 19:52:49
问题 I'm writing a Rust library and I want to provide examples in my documentation that compile as part of running cargo test do not run. Is this possible? I'm writing a database client library, and the examples make use of a hypothetical, non-existing database server. As such, the examples always fail when run, but it's important that the examples be valid syntactically. Hence my requirements above. If there's no way to do what I want, then how does one opt out of having cargo test run a specific

How to opt out of running a doc test?

笑着哭i 提交于 2021-02-18 19:51:00
问题 I'm writing a Rust library and I want to provide examples in my documentation that compile as part of running cargo test do not run. Is this possible? I'm writing a database client library, and the examples make use of a hypothetical, non-existing database server. As such, the examples always fail when run, but it's important that the examples be valid syntactically. Hence my requirements above. If there's no way to do what I want, then how does one opt out of having cargo test run a specific

Writing Diesel CRUD operations for generic types

别说谁变了你拦得住时间么 提交于 2021-02-18 17:34:37
问题 I am trying to write a Rust crate which removes some boilerplate code from the user when creating simple CRUD operations with Diesel For instance, if you have a Diesel Insertable like this one: #[derive(Insertable)] #[table_name = "users"] pub struct UserCreate<'a> { pub email: String, pub hash: &'a [u8], pub first_name: Option<String>, pub family_name: Option<String>, } I want the crate user to just write create<UserCreate>(model, pool) , to insert the struct fields into a database row. To

How to modify the last item of an array?

余生颓废 提交于 2021-02-18 12:14:09
问题 Since arr is borrowed as mutable, the length of arr can't be gotten by calling len() . I'm stuck here, what's the right way to do it? fn double_last(arr: &mut[i32]) -> &i32 { let last = &mut arr[arr.len() - 1]; // borrow checker error. //let last = &mut arr[3]; // fine *last *= 2; last } fn main() { let mut a = [1,2,3,4]; println!("{}", double_last(&mut a)); println!("{:?}", a); } 回答1: This will hopefully be fixed with the introduction of non-lexical lifetimes and the accompanying changes

Concatenate array slices

廉价感情. 提交于 2021-02-18 10:20:12
问题 I have two (very large) arrays foo and bar of the same type. To be able to write some nice code, I would like to obtain a read-only slice, result , of the concatenation of the two arrays. This operation must run in O(1) time and space. Array access for result must also be in O(1). More generally, if result were the concatenation of k array slices, an arbitrary array access for result should run in O( k ). I do not want to copy any elements of foo nor bar . This would seem to be easy to

Differences between a pointer and a reference in Rust

妖精的绣舞 提交于 2021-02-18 07:22:51
问题 A pointer * and a reference & in Rust share the same representation (they both represent the memory address of a piece of data). What's the practical differences when writing code though? When porting C++ code to Rust can they be replaced safely (c++ pointer --> rust pointer, c++ reference --> rust reference ) ? 回答1: Use references when you can, use pointers when you must. If you're not doing FFI or memory management beyond what the compiler can validate, you don't need to use pointers. Both

Reference to element in vector

ⅰ亾dé卋堺 提交于 2021-02-18 04:56:50
问题 I'm having trouble giving a struct a member variable that is a reference to another type. Here is my struct and implementation: struct Player<'a> { current_cell: &'a Cell, } impl<'a> Player<'a> { pub fn new(starting_cell: &'a Cell) -> Player<'a> { Player { current_cell: starting_cell } } } The player has a reference to the current Cell that they are in. Here is my Game struct and its implementation: struct Game { is_running: bool, cells: Vec<Cell>, } impl Game { pub fn new() -> Game { let

What does a scoped lifetime in rust actually mean?

断了今生、忘了曾经 提交于 2021-02-18 02:25:57
问题 So, in: fn v1<'a> (a:~[&'a str]) -> ~[&'a str] { return a; } #[test] fn test_can_create_struct() { let x = v1("Hello World".split(' ').collect()); } I know, I've read http://static.rust-lang.org/doc/master/guide-lifetimes.html#named-lifetimes, but I don't understand what this code actually does . The function is basically parametrized like a generic fn but with a lifetime, is what I've seen said on the IRC channel, but lets imagine that is the case, and we have a L, which is some specific