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.
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:
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:
Is it possible to declare the type of the variable in Rust for loops?
(5 answers)
Closed 3 years ago.
Simple question, can't find an answer anywhere:
for i in 0..65000000000 {
do_something;
}
Throws an error:
literal out of range for i32
Setting this to a larger type also does not work:
for i: u64 in 0..65000000000 {
do_something;
}
Throws an error:
error: missing in in for loop
From the reference :
Syntax
IteratorLoopExpression :
for Pattern in Expression except struct expression BlockExpression
It expects a Pattern not a declaration.
You need to set the type explicitly by changing the input's type.
for i in 0..65000000000u64 {
do_something;
}
This question already has answers here:
How do I disambiguate traits in Rust?
(2 answers)
How to call a method when a trait and struct use the same method name?
(1 answer)
Closed 4 years ago.
Is it possible to implement two traits with conflicting method names in Rust? I know that it gives you a multiple applicable methods in scope error, but is there a way to resolve this? For example, some languages handle multiple inheritance by allowing you to explicitly specify which one method should take precedence
You want universal function call syntax. The following are all equivalent:
let v = 32;
let _ = v.clone();
let _ = Clone::clone(&v);
let _ = <i32 as Clone>::clone(&v);
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?