Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,955 questions

51,897 answers

573 users

How to use function in Rust

7 Answers

0 votes
fn main() {
    f();
}
 
fn f() {
    println!("function: fn f()");
}
 
 
 
/*
run:
 
function: fn f()
 
*/

 



answered Oct 24, 2022 by avibootz
0 votes
fn main() {
    println!("{}", get_value());
}

fn get_value()->f64 {
    23.0/5.0
}



/*
run:

4.6

*/

 



answered Oct 24, 2022 by avibootz
0 votes
fn main() {
   let n:i32 = 14;
   
   to_zero(n); // pass by value
   
   println!("{}", n);
}

fn to_zero(mut number: i32) {
   number = number * 0;
   
   println!("{}", number);
}
 
 
 
/*
run:
 
0
14

*/

 



answered Oct 24, 2022 by avibootz
0 votes
fn main() {
    let mut n:i32 = 14;
    
    to_zero(&mut n); // pass by reference
    
    println!("{}", n);
}

fn to_zero(number:&mut i32) {
   *number =  0;
    
   println!("{}", number);
}



/*
run:

0
0

*/

 



answered Oct 25, 2022 by avibootz
0 votes
fn main() {
   let s:String = String::from("Rust");

   f(s); 
}

fn f(s:String) {
   println!("{}", s);
}




/*
run:

Rust

*/

 



answered Oct 25, 2022 by avibootz
0 votes
fn main() {
    let lang = "Rust".to_string();
    display(lang, 1.67);
}
 
fn display(lang: String, release: f32 ) {
    println!("{}, {}", lang, release)
}
 
 
 
 
/*
run:
 
Rust, 1.67
 
*/

 



answered Mar 2, 2023 by avibootz
0 votes
fn main() {
    println!("{}", add(37, 98));
}
 
fn add(a: u32, b: u32) -> u32 {
    return a + b;
}
 
 
 
/*
run:
 
135
 
*/

 



answered Mar 2, 2023 by avibootz
...