Fix the environment
Before we do anything, let’s set up the Rust environment. This is very simple.
curl https://sh.rustup.rs -sSf | sh
Copy the code
Install any version, be it stable or beta. And then you put some tool chains on it, and you type it at the terminal
rustup target add aarch64-apple-ios x86_64-apple-ios
Copy the code
Personally, I only installed toolchain for A7 and above 64-bit processor, x86_64-apple-ios this is for emulator. There are a couple of other tool chains that you can attach if you need them.
rustup target add armv7-apple-ios armv7s-apple-ios i386-apple-ios
Copy the code
Start with project Rust
For now, create a Rust project, just use Cargo, and type directly at the terminal
mkdir rust-on-ios && cd rust-on-ios
cargo new rs --lib
mkdir ios
Copy the code
You can now see the ios and RS folders in the rust-on-ios directory. Open the lib. rs file in the rs folder SRC and create a “Hello world” to test the effect.
use std::os::raw::{c_char};
use std::ffi::{CString};
#[no_mangle]
pub extern fn say_hello() - > *mut c_char {
CString::new("Hello Rust").unwrap().into_raw()
}
Copy the code
I’ll just write that. The #[no_mangle] here has to be written to make sure that this function is found after compilation. Then we build the header file, and since the Rust project will then be compiled into a library file, a.h file provides the interface.
char *say_hello(void);
Copy the code
We need to modify the Cargo. Toml file to compile the Rust source into a library.
[package]
name = "rs"
version = "0.1.0 from"
authors = ["limit <[email protected]>"]
edition = "2018"
publish = false
[lib]
name = "app"
crate-type = ["staticlib"]
Copy the code
Now let’s go to rs directory and compile this project.
cargo build --target x86_64-apple-ios --release
Copy the code
Once compiled, you’ll find a libapp.a file in the target/x86_64-apple-ios directory. Let’s set up an iOS project.
Creating an iOS project
Now create a Single Page App project. I’m just going to make it easy, so I’m going to build an Objective-C project, or I’m going to build a Swift project, but I’m going to bridge it. Next creates the project along the way, then adds the lib file
Libapp. a is a file we compiled with the Rust project, and libresolv. TBD is used for linking.
To Add libapp.a, simply click the plus sign, then Add Other, and select the compiled libapp.a file. Then place the previously written header file into the project. There was an error when compiling. Since we included libapp.a in the project, the tool didn’t know where the lib file was at compile time, so we had to manually set the search path for the lib file.
Now I should compile it again and it should work.
To demonstrate, use this function in the ViewControll. M file.
#import "ViewController.h"
#import "libapp.h"
@interface ViewController(a)
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fromCStr = @(say_hello());
UILabel *label = [[UILabel alloc]
initWithFrame: (CGRect) { 100.100.100.100 }];
label.text = fromCStr;
[self.view addSubview:label];
}
@end
Copy the code
The emulator should then display the text Hello Rust.
In order to develop mobile terminal applications with Rust, knowledge related to Rust FFI is required. I should write some content related to Rust FFI in the future, and then write UI with Flutter to ensure up development experience.