Rust Playground (rust-lang.org)  線上練習

沒有加上mut是不能變更變數內容的

image

還有文字、bool、f32/f64、char

數字可以加上底線方便閱讀,效果和沒底線是一樣的。

fn main() {
    let mut x=10;
    println!("Hello, world! {x}");
    x=1_20;
    println!("Hello, world! {x}");
}

Hello, world! 10
Hello, world! 120   
let s = String::from("初始內容");  

字串的串接>>

let mut word1=String::from("go go ");   
println!("word1 --> {word1}");   
let word2=" super man !! ";    
word1.push_str(word2);  //push是用來塞一個字元的
println!("word1 --> {word1}");


word1 --> go go 
word1 --> go go  super man !! 

    let str1=String::from("字串1");
    let str2=String::from("_字串2");
    let str3=String::from("_字串3");
    let str4=str1+&str2+&str3; //str1被移動過就不能用了,str2的&不能移除,str1不能加上&
    
    println!("字串相加測試: {str4}");
    println!("字串相加測試: {str2}");

 

字串相加測試: 字串1_字串2_字串3
字串相加測試: _字串2

 

 

arrow
arrow
    全站熱搜

    vsvs 發表在 痞客邦 留言(0) 人氣()