added function and variable lessons.
This commit is contained in:
		
							parent
							
								
									4106843d70
								
							
						
					
					
						commit
						405437686a
					
				
					 6 changed files with 118 additions and 0 deletions
				
			
		
							
								
								
									
										66
									
								
								the_book/branches/src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								the_book/branches/src/main.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,66 @@
 | 
			
		|||
fn main() {
 | 
			
		||||
    let condition = true;
 | 
			
		||||
    let number = if condition { 5 } else { 6 };
 | 
			
		||||
 | 
			
		||||
    if number % 4 == 0 {
 | 
			
		||||
        println!("number is divisible by 4");
 | 
			
		||||
    } else if number % 3 == 0 {
 | 
			
		||||
        println!("number is divisible by 3");
 | 
			
		||||
    } else if number % 2 == 0 {
 | 
			
		||||
        println!("number is divisible by 2");
 | 
			
		||||
    } else {
 | 
			
		||||
        println!("number is not divisible by 4, 3, or 2");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //breaking out of multiple layers.
 | 
			
		||||
    let mut count = 0;
 | 
			
		||||
    'counting_up: loop {
 | 
			
		||||
        println!("count = {}", count);
 | 
			
		||||
        let mut remaining = 10;
 | 
			
		||||
 | 
			
		||||
        loop {
 | 
			
		||||
            println!("remaining = {}", remaining);
 | 
			
		||||
            if remaining == 9 {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            if count == 2 {
 | 
			
		||||
                break 'counting_up;
 | 
			
		||||
            }
 | 
			
		||||
            remaining -= 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        count += 1;
 | 
			
		||||
    }
 | 
			
		||||
    println!("End count = {}", count);
 | 
			
		||||
 | 
			
		||||
    //returning loop
 | 
			
		||||
    let mut counter = 0;
 | 
			
		||||
 | 
			
		||||
    let result = loop {
 | 
			
		||||
        counter += 1;
 | 
			
		||||
 | 
			
		||||
        if counter == 10 {
 | 
			
		||||
            break counter * 2;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    println!("The result is {}", result);
 | 
			
		||||
 | 
			
		||||
    //while loop
 | 
			
		||||
    let mut number = 3;
 | 
			
		||||
 | 
			
		||||
    while number != 0 {
 | 
			
		||||
        println!("{}!", number);
 | 
			
		||||
 | 
			
		||||
        number -= 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("LIFTOFF!!!");
 | 
			
		||||
 | 
			
		||||
    //for loop
 | 
			
		||||
    let a = [10, 20, 30, 40, 50];
 | 
			
		||||
 | 
			
		||||
    for element in a {
 | 
			
		||||
        println!("the value is: {}", element);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue