This question already has answers here:
What is the relation between auto-dereferencing and deref coercion?
(1 answer)
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed 2 years ago.
All the commented lines below are valid. How do multiple & have the same impact? Does Rust do some magic to interpret multiple &s?
// All commented code is also valid - wondering how?
let string_a = String::from("String A");
let str_a: &str = &string_a;
// let str_a: &str = &&&&&string_a; // --> this is valid statement
let mut str_option: Option<&str>;
str_option = Some(str_a);
// str_option = Some(&str_a); // --> this is valid statement
// str_option = Some(&&str_a); // --> this is also valid statement
Related
This question already has answers here:
Confusion over auto-dereferencing rules when using function
(1 answer)
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed last month.
Let's say I have such a function:
fn take_by_ref(i:&i32){
println!("{}",i);
}
Now I want to pass mutable reference there:
#[test]
fn test_mut(){
let mut x = 9;
let m = &mut x;
}
And it is unclear what is the difference between the two calls to this function?
first:
take_by_ref(m);
second:
take_by_ref(&*m);
Are these two calls equal and the compiler makes first call as the second one automatically? or is there a difference?
And won't the rule that either mut reference or immutable can exist be violated here when call take_by_ref?
That is, the question is that when passing a mut variable to such a function, it can become auto-immutable?
This question already has answers here:
Why is it possible to implement Read on an immutable reference to File?
(1 answer)
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed 2 years ago.
I'm playing with a function that handles a TCP connection:
use std::{io::Write, net::TcpStream, sync::Arc};
fn handle_connection(stream: TcpStream) {
let data = "Hello!\n";
let stream_arc = Arc::new(stream);
let mut s: &TcpStream = &*stream_arc; //let mut s = Deref::deref(&stream_arc);
s.write_all(data.as_bytes()).unwrap(); //same as Write::write_all(&mut s, data.as_bytes()).unwrap();
}
I can't figure out why Rust doesn't complain about Write::write_all since s is not a mutable reference. I noticed also that removing mut from s compilation fails. Am I missing some trait implementation that permits this? Is this a way to mutably borrow from std::sync::Arc?
This question already has answers here:
How to create a String directly?
(3 answers)
What is the difference between these 3 ways of declaring a string in Rust?
(1 answer)
How do I convert between String, &str, Vec<u8> and &[u8]?
(1 answer)
Closed 2 years ago.
In Rust, there are several ways to create a String from a string literal:
fn main() {
let s_from = String::from("string"); // type on the right of the operator
let s_into: String = "string".into(); // type on the left of the operator
let s_to_string = "string".to_string(); // expresses type
let s_to_owned = "string".to_owned(); // expresses ownership
assert_eq!(s_from, s_into);
assert_eq!(s_from, s_to_string);
assert_eq!(s_from, s_to_owned);
}
Is there a rule in rust to follow a reading direction in relation to the operator?
Is there a reason to favour From/Into over to_string()/to_owned()?
Is there a reason to favour one of those over all the others?
With several developers working on a project, a mixture usage of those happens.
This question already has answers here:
Meaning of '&variable' in arguments/patterns
(1 answer)
What's the difference between ref and & when assigning a variable from a reference?
(3 answers)
How do references work in patterns in binding expressions? [duplicate]
(1 answer)
Closed 3 years ago.
The title might not be appropriate, be here is an example:
fn foo(impl Fn(&u32) -> bool) { ... }
foo(|x| *x < 100);
foo(|&x| x < 100);
Are the two closures passed to foo equivalent? I saw people use the second form in some places, but I couldn't find it in the official book. Is the &x part a destructure...?
This is called reference pattern:
ReferencePattern :
(&|&&) mut? Pattern
Reference patterns dereference the pointers that are being matched and, thus, borrow them.
So yes, they are equivalent.
This question already has answers here:
What is the return type of the indexing operation?
(2 answers)
How to get subslices?
(1 answer)
Closed 5 years ago.
I have a function that converts a wide string array as a [u16] to a String.
fn get_string_from_wide(wide_array: &[u16]) -> Result<String, String> {
let trimmed_wide = wide_array.iter()
.position(|char| *char == 0)
.map(|i| &wide_array[..i]) //<- remove `&` will give the error as the title
.unwrap_or(wide_array);
let os_str = OsString::from_wide(trimmed_wide);
os_str
.into_string()
.or(Err("Could not convert `Os_String` to `String`".to_string()))
}
Although the parameter is passed by reference, I still get the type as [u16] instead of &[u16]
If I remove the & the code compiles, but I don't want to pass a reference of a reference.
Is the var wide_array actually the data itself and not a reference as stated in the function parameter, and thus I still need to take a reference with & for referenced function calls? Or is it actually a reference to it?