Rust application development in the OpenWrt router system of the MIPS platform, openwrtrust
Author: Liigo (Zhuang Xiaoli)
Date: January 1, September 17, 2014
Original: http://blog.csdn.net/liigo/article/details/39347541
Copyright, reproduced please indicate the source: http://blog.csdn.net/liigo
Target
Use the Rust language to develop application software on the MIPS (el) + OpenWrt router platform.
Compile rustc
First, compile the source code of the R
configuration editor or IDE is beyond the scope of this tutorial, see the other tutorials separately.Gossip Less, we first create a new folder into the projects.$ mkdir ~/projects$ CD ~/projects$ mkdir Hello_world$ CD Hello_worldIf you are on the Windows platform and do not use PowerShell, there may be an error. Check out other documentation to learn how to use your shell.Next, let's create a new source file. We named it main.rs. Rust files always have a. rs extension. If you use more than two
About the data source of the number of Rust code lines in the Servo project, servorust
A blog I wrote two months ago, "Why do I say Rust is a reliable programming language?" (hereinafter referred to as the original article), in which "6. two semi-large Success Stories "I wrote:
Servo: Next-generation browser rendering engine (Webkit/Blink), with over 0.4 million lines of Rust code
Rustc + std: Rust compiler and standard library, with over 0.35 mil
manage memory, such asRust was first unveiled at the 7- month- old Mozilla Community Summit, when people asked if they would use Rust rewrite Firefox , Brenda say I hope so. Rust is still at the beginningauthor::Nickname:Old Wow's claws(Full Name::AttilaxAkbar Al Rapanui Attilaksachanui)Kanji Name:Ayron, email:[email protected] Reprint please indicate source: http://www.cnblogs.com/attilax/2.no Null pointers appear, etc. 2.1.1.Rust 1.1 Stable version update content·The compilation speed is i
HelloWorld and rusthelloworld in Rust LanguageHelloWorld of Rust Language
Reference:
Http://doc.crates.io/guide.html1. What is Cargo?
Similar to maven/ant in java, automake in c, and Cargo is a rust project management tool. Use Cargo to do four things:
1) Configuration Management
2) download project Dependencies
3) Call the compiler rustc to compile and release the program.
4) In short, the development of rust's package solution
When you install rust,
the module is tested, so add_two_0 is the function test. The name will automatically grow, such as add_two_1 and so on.Page 834.3-piece compilationRust has a special property, #[cfg], and you can control the compiler by its identity in two ways:#[cfg (foo)]#[cfg (bar= "Baz")]It also has some help options:#[cfg (Any (unit, Windows))]#[cfg (All (unit, target_pointer_width= "32"))]#[cfg (not (foo))]can also be nested:#[cfg (No (Unix), All (target_os= "MacOS", Target_arch = "PowerPC"))]As for how t
! '][panic] so that It is easier-discuss failure in the context of the error handling Without making clarifications as to whether you is referring to The ' fail ' macro or failure more generally. * on Linux, ' osrng ' prefers the new, more reliable ' getrandom ' Syscall when available. * the ' serialize ' crate has been renamed ' Rustc-serialize ' and Moved out of the distribution to Cargo. Although it is widely Used now, it's expected to being superc
nightly version, it is best to bypass the first.) )Modify PATH variable? (y/n)YThe tool chain includes: 1. RUSTC 2. Cargo 3. Rustup
Add Rust Home Directory%userprofile%.cargo\bin to the operating system's PATH environment variable
Verify installation success: Rustc–version
Rust IDE
Vscode currently supports rust development Extensions rust code and native Debug
Install RLS Reference:Rustup Self Update
Rust
) return to
elsif (val = @cache [at]). Present?
Return Val End total
= 1
parent = 1
gp = 1
(1..at).
each do |i| Total = parent + GP
gp = parent
parent = Total
-
This is the result of its operation:
$ time PHP php_fib.php real
0m2.046s
user 0m1.823s
sys 0m0.207s
Now let's do the rust version. The following are library resources:
Copy Code code as follows:
#! [Crate_type = "Staticlib"]
fn FIB (at:usize)-> usize {
If at = = 0 {
return 0;
extern "C" FN rust_fib (at:usize)-> usize {
FIB (at)
}
Note that I compiled the library rustc–o rust_lib.rs to make the compiler optimize (because we are here standard). Here is the C extension source (related excerpt):
Php_function (confirm_rust_fib_compiled)
{
Long number;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "L", number) = = failure) {
Return
}
Return_long (RUST_FIB (number));
}
To run a PHP s
::*;use iron::status;fn main() { fn hello_world(_: mut Request) -> IronResult
Then run the compilation command below hello_world:
# cargo build# cargo run
Running 'target/debug/hello_world'Open the browser and check: localhost: 3000
Hello World!
Several terminologies of Rust:
Cargo: The construction tool of the rust language. It is very image-oriented. It refers to trucks.
Crate: equivalent to java jar package, so of c/c ++. Wicker boxes. Various wicker boxes are installed in the van.
the Eclipse plugin and racer to develop the rust project, you must download the rust source code. The source code can also be downloaded to the Rust website to guarantee a version of 1.1 downloads. If the download speed is slow, you can also click to open the Baidu cloud disk for download.Next you can extract the rust source, and I'll unzip it into my own Rust project folder E:\Program\Rust\ .Finally set the environment variable of the rust source path RUST_SRC_PATH=E:\Program\Rust\
0m0.207s
Now let's make the Rust version. Below are the library resources:
#![crate_type = "staticlib"]fn fib(at: usize) -> usize { if at == 0 { return 0; } else if at == 1 { return 1; } let mut total = 1; let mut parent = 1; let mut gp = 0; for _ in 1 .. at { total = parent + gp; gp = parent; parent = total; } return total;}#[no_mangle]pub extern "C" fn rust_fib(at: usize) -> usize { fib(at)}
Note that the compiled Libr
;} #[no_mangle]pub extern "C" FN RUST_FIB (at:usize), usize { fib (at)}
Note that I compiled the library rustc–o rust_lib.rs to make the compiler optimized (because we are the standard here). Here is the C extension source (related excerpt):
Php_function (confirm_rust_fib_compiled) { long number; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "L", number) = = FAILURE) { return; } Return_long (RUST_FIB (number));}
To run t
0m2.046suser 0m1.823ssys 0m0.207s
Now let's do the rust version. Here is the Library resource:Copy the Code code as follows:#! [Crate_type = "Staticlib"]fn FIB (at:usize), usize {If at = = 0 {return 0;} else if at = = 1 {return 1;}Let mut total = 1;Let mut parent = 1;Let mut gp = 0;For _ in 1.. at {Total = parent + GP;GP = parent;parent = total;}return total;}#[no_mangle]Pub extern "C" FN RUST_FIB (at:usize), usize {FIB (at)}
Note that I compiled the library
D language of digital Mars. Their common feature is the high productivity of grammar and the efficiency of full compilation.
Well, let's start with a hello,world program to say hello to the rust language.First go to the official website download Rust Package: https://www.rust-lang.orgOn the Mac, just use the brew install rust.IDE, I'm using visual Studio Code and you're free to choose.
fn Main () {let
s = "hello,rust!";
println! ("{}", s);
}
Call R
help you identify some of the points that may occur, Then compile the failure to force you to change the code to a more secure wording, the requirements of the compiler is very strict, beginners basically is every write a piece of code on his knees to ask RUSTC in the end is right, and kneeling to ask Google exactly why I wrote the wrong. During this time in rust, the rust compiler corrected many of my bad programming habits and taught the code to be
:
$ time php php_fib.php real 0m2.046suser 0m1.823ssys 0m0.207s
Now let's make the Rust version. Below are the library resources:
The code is as follows:
#! [Crate_type = "staticlib"] Fn fib (at: usize)-> usize {If at = 0 {Return 0;} Else if at = 1 {Return 1;} Let mut total = 1;Let mut parent = 1;Let mut gp = 0;For _ in 1 .. {Total = parent + gp;Gp = parent;Parent = total;} Return total;} # [No_mangle]Pub extern "C" fn rust_fib (at: usize)-> usize {Fib ()}
Note that the compiled Library
-friendly, however, when the compiler cannot predict the lifetime of an object, you must manually specify it. To be honest, it is also a headache for unfamiliar people.
2. rust has very high requirements on code security. It provides various syntax constraints (for example, a variable cannot be borrow as mutable more than once ), the compiler can help you find Bugs during compilation, and the compilation failure forces you to change the code to a safer way. The requirements of the compiler are v
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.