ฟังก์ชั่นในภาษา Rust
บทความนี้จะพาผู้อ่านไปสร้างฟังก์ชั่นในภาษา Rust กันครับ
ในการเขียนและสร้างฟังก์ชั่นในภาษา Rust จะเหมือนกับภาษา C/C++ โดยฟังก์ชั่นหลัก หรือ ฟังก์ชั่น main เขียนฟังก์ชั่นนี้ได้ตามนี้
เหมือนที่เราเขียนโปรแกรม Hello, world! ในบทความที่ผ่านมาครับ
นอกจากนั้นเรายังสามารถสร้างฟังก์ชั่นในภาษา Rust ได้เหมือนกับภาษา C/C++ ครับ มีหลักการดังนี้
เราจะได้ฟังก์ชั่น print_number แต่ยังไม่สามารถนำไปใช้งานได้ครับ ต้องเรียกใช้ฟังก์ชั่น print_number ในฟังก์ชั่น main ก่อนครับ
ขอบคุณครับ
ในการเขียนและสร้างฟังก์ชั่นในภาษา Rust จะเหมือนกับภาษา C/C++ โดยฟังก์ชั่นหลัก หรือ ฟังก์ชั่น main เขียนฟังก์ชั่นนี้ได้ตามนี้
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
} |
เหมือนที่เราเขียนโปรแกรม Hello, world! ในบทความที่ผ่านมาครับ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("Hello, world!"); | |
} |
fn ชื่อฟังก์ชั่น() {ตัวอย่าง
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn print_number(x: i32) { | |
println!("x is: {}", x); | |
} |
เราจะได้ฟังก์ชั่น print_number แต่ยังไม่สามารถนำไปใช้งานได้ครับ ต้องเรียกใช้ฟังก์ชั่น print_number ในฟังก์ชั่น main ก่อนครับ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
print_number(5); | |
} | |
fn print_number(x: i32) { | |
println!("x is: {}", x); | |
} |
ขอบคุณครับ
ความคิดเห็น
แสดงความคิดเห็น