Skip to content

Instantly share code, notes, and snippets.

@takurx
Last active October 10, 2022 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takurx/a34e8231323b658953838f61f187a9b4 to your computer and use it in GitHub Desktop.
Save takurx/a34e8231323b658953838f61f187a9b4 to your computer and use it in GitHub Desktop.
test function Rust language

log

chiya@ujimatsu:~/Works/hello_rust/hello_rust_function$ cargo run
   Compiling hello_rust v0.1.0 (/home/chiya/Works/hello_rust/hello_rust_function)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/hello_rust`
Hello, world! 1
    odd number
Hello, world! 2
    even number
Hello, world! 3
    odd number
Hello, world! 4
    even number
H
e
l
l
o

code

fn main() {
    let str_hello = "Hello";
    let str_world = "world!";
    let str_hello_world = String::from(str_hello) + ", " + str_world;
    let str_even = "even number";
    let str_odd = "odd number";

    for n in 1..5 {
        print_even_or_odd(n, &str_hello_world);
        //let even_or_odd = is_even_or_odd_by(n); 
        if is_even_or_odd_by(n) {
            println!("    {}", str_even);
        }
        else {
            println!("    {}", str_odd);
        }
    }

    for i in str_hello.chars() {
        println!("{}", i);
    }
}

fn print_even_or_odd(n: u32, str_hello_world: &String) -> (){
    println!("{} {}", str_hello_world, n);
}

fn is_even_or_odd_by(n: u32) -> bool{
    if n%2 == 0 {
        return true;
    }
    else {
        return false;
    }
    //return false;
}

log

chiya@ujimatsu:~/Works/hello_rust/hello_rust_function$ cargo run
   Compiling hello_rust v0.1.0 (/home/chiya/Works/hello_rust/hello_rust_function)
warning: unreachable statement
  --> src/main.rs:35:5
   |
30 |         return true;
   |         ----------- any code following this expression is unreachable
...
35 |     return false;
   |     ^^^^^^^^^^^^^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

warning: `hello_rust` (bin "hello_rust") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/hello_rust`
Hello, world! 1
    odd number
Hello, world! 2
    even number
Hello, world! 3
    odd number
Hello, world! 4
    even number
H
e
l
l
o

code

......
......
fn is_even_or_odd_by(n: u32) -> bool{
    if n%2 == 0 {
        return true;
    }
    else {
        return false;
    }
    return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment