顯示具有 Rust 標籤的文章。 顯示所有文章
顯示具有 Rust 標籤的文章。 顯示所有文章

2016年12月27日 星期二

[Rust] 格式化輸出

Rust也可以像C的printf一樣格式化輸出


fn main()
{
    let x=10*5;
    println!("x={}",x);
}


輸出為:

x=50


試試字串

fn main()
{
    let name="boywhy";
    println!("My Name is {}",name);
}

輸出為:


My Name is boywhy


也可以透過指定Index的方式輸出



fn main()
{
  println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
}

輸出為:


Alice, this is Bob. Bob, this is Alice


也可以透過命名參數來格式化輸出


fn main()
{
    println!("{subject} {verb} {object}",
             object="the lazy dog",
             subject="the quick brown fox",
             verb="jumps over");
}

輸出為:


the quick brown fox jumps over the lazy dog


也可以直接指定輸出為進制 ,例如{:b}


fn main()
{
  println!("{} of {:b} people know binary, the other half don't", 1, 2);  
}


1 of 10 people know binary, the other half don't

共輸出長度為6的number,所以輸出了5個空白以及1個數值


fn main()
{
 println!("{number:>width$}", number=1, width=6);i
}

輸出為      1


也可以輸出補0,差別在width$前面要補0


fn main()
{
 println!("{number:>0width$}", number=1, width=6);i
}

輸出為:


000001




參考資料:

1.2 Formatted print

2016年12月26日 星期一

[Rust] Mac OSX Install Rust

紀錄一下在Mac安裝Rust語言的流程。

(1)輸入curl https://sh.rustup.rs -sSf | sh  會出現如下圖



































沒有特別情況的話,按下1以及Enter, 讓它以預設值安裝




































安裝完畢




































接下來要設環境變數,不然會找不到指令

編輯~/.bash_profile








增加一行export PATH="$PATH:"/Users/使用者名稱/.cargo/bin




























存檔後,關掉終端機重新開啟,輸入echo "$PATH" 確定路徑有成功寫入













我們來寫個範例吧


fn main()
{
    println!("Hello WOrld!")
}
~    

















編譯並執行


$ rustc hello.rs 
$ ./hello 


















參考來源:

https://www.rust-lang.org/en-US/install.html