CS152
Chris Pollett
Oct 4, 2021
fn main() { let 我的变量1 = "hello"; println!("{}, world!", 我的变量1); }
/// #Using Markdown this is an H1 heading comment describing the super_awesome function /// /// As you might expect, the super_awesome does something awesome. fn super_awesome { // code for super_awesome. }
//! #My Crate //! //! `my_crate` is a collection structs hand-crafted from 1's and 0's
rustdoc name_of_rust_file.rs
cargo doc
cargo doc --open
fn main () { let x = 7; x = x + 1; println!("The value of x is: {}", x); }would give the error:
error[E0384]: cannot assign twice to immutable variable `x` --> immutable_test.rs:3:3 | 2 | let x = 7; | - | | | first assignment to `x` | help: consider making this binding mutable: `mut x` 3 | x = x + 1; | ^^^^^^^^^ cannot assign twice to immutable variable
fn main () { let x = 7; let x = x + 1; // On the right hand, x is from outer scope. On the left hand, // it is a new shadowing variable for x from the new let scope println!("The value of x is: {}", x); }
fn main () { let mut x = 7; x = x + 1; println!("The value of x is: {}", x); }
let name_of_variable : some_type = some_value;For example,
let x:u32 = 755; // x will be a 32 bit unsigned int
use std::any::type_name; fn type_of(_: T) -> &'static str { //magic for now type_name:: () } fn main () { let x = 7; // since 7 is of type i32, x will be inferred to be of type i32 println!("The value of x is: {} and its type is {}", x, type_of(x)); }
let flag:bool; flag = true;
const FOO: i32 = 37; //type of constant must be declared fn main() { println!("{}", FOO); /* let FOO = 77; // this would cause an error if not commented out as // can't reassign a constant println!("{}", FOO); */ }
static FOO: u32 = 37;
Which of the following statements is true?
"hello"; // string type '5'; // character type 5; // integer type
let x = (7+3) - 2;
assert!(123 == 123); //assert! does nothing on true, gives backtrace on false assert!("hello" <= "world");
let x = false || true; // true let y = false && panic!(); // operator is lazy, since if left argument is false, // doesn't evaluate right. I.e., 'panic!()' not evaluated
use std::env; //use this to get command line arguments fn main() { let args: Vec<_> = env::args().collect(); if args.len() > 1 { let a:u32 = args[1].parse().unwrap(); //converts to int if a > 10 { //notice: no parentheses println!("a is big"); } else if a <= 10 && a > 5 { println!("a is medium"); } else { println!("a is small"); } } else { println!("I need arguments"); } }
fn main() { let condition = true; let number = if condition { 5 //this is the return value of the block } else { 6 // types of all branches must be same to work } println!("The value of number is: {}", number); }
let x = 4; match x { 1 => println!("one"), 2 => println!("two"), 3 => println!("three"), _ => println!("that number is too big!"); }
fn main () { loop { println!("Say this over and over."); } }
fn main () { let mut i = 0; let x = loop { println!("Say this over and over."); if i > 10 { break i; } i+= 1; }; println!("x = {}", x); }
fn main () { let mut count_down = 10; while count_down > 0 { println!("{}!", count_down); count_down -= 1; } println!("Blast Off!"); }
fn main() { for i in 1..10 { //doesn't include 10 println!("{}", i); } for i in 1..=10 { //includes 10 println!("{}", i); } for i in (1..10).step_by(2) { //1 3 5 7 9 println!("{}", i); } let m = 60; for i in m/2..m { //30, 31,..., 59 println!("{}", i); } }
fn name_of_function(var1:type1, ... var_n:type_n) -> return_type { //code return some_value; }
fn add_plus_one( x:i32, y:i32) -> i32 { return x + y + 1; }
fn add_plus_one( x:i32, y:i32) -> i32 { x + y + 1 //does the same as above, notice no ; }